<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>智慧农业——驱动程序设计 | XiaoZiYan</title><meta name="keywords" content="智慧农业,PCB,klie"><meta name="author" content="XiaoZiYan"><meta name="copyright" content="XiaoZiYan"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="智慧农业PCBkile">
<meta property="og:type" content="article">
<meta property="og:title" content="智慧农业——驱动程序设计">
<meta property="og:url" content="https://xiaoziyanxiaoludan.gitee.io/xiaoziyan/2021/08/03/%E6%99%BA%E6%85%A7%E5%86%9C%E4%B8%9A%E9%A9%B1%E5%8A%A8%E7%A8%8B%E5%BA%8F%E5%BC%80%E5%8F%91/index.html">
<meta property="og:site_name" content="XiaoZiYan">
<meta property="og:description" content="智慧农业PCBkile">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://xiaoziyan.top/images/a7944f919383883f23a8b3f45788ae9c.jpeg">
<meta property="article:published_time" content="2021-08-03T07:12:02.000Z">
<meta property="article:modified_time" content="2022-11-03T10:29:18.005Z">
<meta property="article:author" content="XiaoZiYan">
<meta property="article:tag" content="智慧农业">
<meta property="article:tag" content="PCB">
<meta property="article:tag" content="klie">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://xiaoziyan.top/images/a7944f919383883f23a8b3f45788ae9c.jpeg"><link rel="shortcut icon" href="/xiaoziyan/img/favicon.png"><link rel="canonical" href="https://xiaoziyanxiaoludan.gitee.io/xiaoziyan/2021/08/03/%E6%99%BA%E6%85%A7%E5%86%9C%E4%B8%9A%E9%A9%B1%E5%8A%A8%E7%A8%8B%E5%BA%8F%E5%BC%80%E5%8F%91/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta/><link rel="stylesheet" href="/xiaoziyan/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/xiaoziyan/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: XiaoZiYan","link":"链接: ","source":"来源: XiaoZiYan","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#121212","position":"bottom-left"},
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: true,
  islazyload: false,
  isanchor: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '智慧农业——驱动程序设计',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-11-03 18:29:18'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="./css/custom.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome/css/font-awesome.min.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/hexo-electric-clock@1.0.6/clock.css"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.4.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/xiaoziyan/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/xiaoziyan/archives/"><div class="headline">文章</div><div class="length-num">33</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/xiaoziyan/tags/"><div class="headline">标签</div><div class="length-num">41</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/xiaoziyan/categories/"><div class="headline">分类</div><div class="length-num">39</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/xiaoziyan/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 多媒体</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/xiaoziyan/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/xiaoziyan/movies/"><i class="fa-fw fas fa-video"></i><span> 视频</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('http://xiaoziyan.top/images/a7944f919383883f23a8b3f45788ae9c.jpeg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/xiaoziyan/">XiaoZiYan</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/xiaoziyan/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 多媒体</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/xiaoziyan/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/xiaoziyan/movies/"><i class="fa-fw fas fa-video"></i><span> 视频</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">智慧农业——驱动程序设计</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-08-03T07:12:02.000Z" title="发表于 2021-08-03 15:12:02">2021-08-03</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-11-03T10:29:18.005Z" title="更新于 2022-11-03 18:29:18">2022-11-03</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/xiaoziyan/categories/%E6%99%BA%E6%85%A7%E5%86%9C%E4%B8%9A/">智慧农业</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/xiaoziyan/categories/%E6%99%BA%E6%85%A7%E5%86%9C%E4%B8%9A/PCB/">PCB</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/xiaoziyan/categories/%E6%99%BA%E6%85%A7%E5%86%9C%E4%B8%9A/PCB/kile/">kile</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="智慧农业——驱动程序设计"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h3 id="点亮一个LED灯-C"><a href="#点亮一个LED灯-C" class="headerlink" title="点亮一个LED灯(.C)"></a>点亮一个LED灯(.C)</h3><p>写程序之前先试着点亮一个LED灯，用于测试，这里LED灯连接的是PC13口，所以要使能GPIOC时钟，并拉低PC13口</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&quot;led.h&quot;</span></span></span><br><span class="line">	    </span><br><span class="line"><span class="comment">//LED IO初始化</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">LED_Init</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"> </span><br><span class="line"> GPIO_InitTypeDef  GPIO_InitStructure;</span><br><span class="line"> </span><br><span class="line"> RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);	 <span class="comment">//使能PC端口时钟</span></span><br><span class="line">	</span><br><span class="line"> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;				 </span><br><span class="line"> GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 <span class="comment">//推挽输出</span></span><br><span class="line"> GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 <span class="comment">//IO口速度为50MHz</span></span><br><span class="line"> GPIO_Init(GPIOC, &amp;GPIO_InitStructure);					 <span class="comment">//根据设定参数初始化GPIOC.13</span></span><br><span class="line"> GPIO_SetBits(GPIOC,GPIO_Pin_13);						 <span class="comment">//PC.13 输出高</span></span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"> </span><br></pre></td></tr></table></figure>

<h3 id="点亮一个LED灯（-h）"><a href="#点亮一个LED灯（-h）" class="headerlink" title="点亮一个LED灯（.h）"></a>点亮一个LED灯（.h）</h3><p>在led.h文件定义PC13为LED0方便在程序调用。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">ifndef</span> __LED_H</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> __LED_H	 </span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&quot;sys.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> LED0 PCout(13)<span class="comment">// PC5</span></span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">LED_Init</span><span class="params">(<span class="keyword">void</span>)</span></span>;<span class="comment">//初始化	 				    </span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br></pre></td></tr></table></figure>

<h3 id="dht11温湿度传感器"><a href="#dht11温湿度传感器" class="headerlink" title="dht11温湿度传感器"></a>dht11温湿度传感器</h3><h5 id="复位DHT11"><a href="#复位DHT11" class="headerlink" title="复位DHT11"></a>复位DHT11</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">DHT11_Rst</span><span class="params">(<span class="keyword">void</span>)</span>	   </span></span><br><span class="line"><span class="function"></span>&#123;                 </span><br><span class="line">		DHT11_IO_OUT(); 	<span class="comment">//SET OUTPUT</span></span><br><span class="line">    DHT11_DQ_OUT=<span class="number">0</span>; 	<span class="comment">//拉低DQ</span></span><br><span class="line">    delay_ms(<span class="number">20</span>);    	<span class="comment">//拉低至少18ms</span></span><br><span class="line">    DHT11_DQ_OUT=<span class="number">1</span>; 	<span class="comment">//DQ=1 </span></span><br><span class="line">		delay_us(<span class="number">30</span>);     	<span class="comment">//主机拉高20~40us</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="等待DHT11回应"><a href="#等待DHT11回应" class="headerlink" title="等待DHT11回应"></a>等待DHT11回应</h5><p>等待DHT11的回应<br>返回1:未检测到DHT11的存在<br>返回0:存在</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">u8 <span class="title">DHT11_Check</span><span class="params">(<span class="keyword">void</span>)</span> 	   </span></span><br><span class="line"><span class="function"></span>&#123;   </span><br><span class="line">	u8 retry=<span class="number">0</span>;</span><br><span class="line">	DHT11_IO_IN();<span class="comment">//SET INPUT	 </span></span><br><span class="line">    <span class="keyword">while</span> (DHT11_DQ_IN&amp;&amp;retry&lt;<span class="number">100</span>)<span class="comment">//DHT11会拉低40~80us</span></span><br><span class="line">	&#123;</span><br><span class="line">		retry++;</span><br><span class="line">		delay_us(<span class="number">1</span>);</span><br><span class="line">	&#125;;	 </span><br><span class="line">	<span class="keyword">if</span>(retry&gt;=<span class="number">100</span>)<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">else</span> retry=<span class="number">0</span>;</span><br><span class="line">    <span class="keyword">while</span> (!DHT11_DQ_IN&amp;&amp;retry&lt;<span class="number">100</span>)<span class="comment">//DHT11拉低后会再次拉高40~80us</span></span><br><span class="line">	&#123;</span><br><span class="line">		retry++;</span><br><span class="line">		delay_us(<span class="number">1</span>);</span><br><span class="line">	&#125;;</span><br><span class="line">	<span class="keyword">if</span>(retry&gt;=<span class="number">100</span>)<span class="keyword">return</span> <span class="number">1</span>;	    </span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="dht11初始化"><a href="#dht11初始化" class="headerlink" title="dht11初始化"></a>dht11初始化</h5><p>初始化DHT11的IO口 DQ 同时检测DHT11的存在<br>返回1:不存在<br>返回0:存在 </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">   	 <span class="function">u8 <span class="title">DHT11_Init</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;	 </span><br><span class="line"> 	GPIO_InitTypeDef  GPIO_InitStructure;</span><br><span class="line"> 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);	 <span class="comment">//使能PA端口时钟</span></span><br><span class="line"> 	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;				 <span class="comment">//PA0端口配置</span></span><br><span class="line"> 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 <span class="comment">//推挽输出</span></span><br><span class="line"> 	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;</span><br><span class="line"> 	GPIO_Init(GPIOA, &amp;GPIO_InitStructure);				 <span class="comment">//初始化IO口</span></span><br><span class="line"> 	GPIO_SetBits(GPIOA,GPIO_Pin_0);						 <span class="comment">//PA0 输出高	    </span></span><br><span class="line">	DHT11_Rst();  <span class="comment">//复位DHT11</span></span><br><span class="line">	<span class="keyword">return</span> DHT11_Check();<span class="comment">//等待DHT11的回应</span></span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>

<h5 id="读取数据"><a href="#读取数据" class="headerlink" title="读取数据"></a>读取数据</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//从DHT11读取一个位</span></span><br><span class="line"><span class="comment">//返回值：1/0</span></span><br><span class="line"><span class="function">u8 <span class="title">DHT11_Read_Bit</span><span class="params">(<span class="keyword">void</span>)</span> 			 </span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"> 	u8 retry=<span class="number">0</span>;</span><br><span class="line">	<span class="keyword">while</span>(DHT11_DQ_IN&amp;&amp;retry&lt;<span class="number">100</span>)<span class="comment">//等待变为低电平</span></span><br><span class="line">	&#123;</span><br><span class="line">		retry++;</span><br><span class="line">		delay_us(<span class="number">1</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	retry=<span class="number">0</span>;</span><br><span class="line">	<span class="keyword">while</span>(!DHT11_DQ_IN&amp;&amp;retry&lt;<span class="number">100</span>)<span class="comment">//等待变高电平</span></span><br><span class="line">	&#123;</span><br><span class="line">		retry++;</span><br><span class="line">		delay_us(<span class="number">1</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	delay_us(<span class="number">40</span>);<span class="comment">//等待40us</span></span><br><span class="line">	<span class="keyword">if</span>(DHT11_DQ_IN)<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">return</span> <span class="number">0</span>;		   </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//从DHT11读取一个字节</span></span><br><span class="line"><span class="comment">//返回值：读到的数据</span></span><br><span class="line"><span class="function">u8 <span class="title">DHT11_Read_Byte</span><span class="params">(<span class="keyword">void</span>)</span>    </span></span><br><span class="line"><span class="function"></span>&#123;        </span><br><span class="line">    u8 i,dat;</span><br><span class="line">    dat=<span class="number">0</span>;</span><br><span class="line">	<span class="keyword">for</span> (i=<span class="number">0</span>;i&lt;<span class="number">8</span>;i++) </span><br><span class="line">	&#123;</span><br><span class="line">   		dat&lt;&lt;=<span class="number">1</span>; </span><br><span class="line">	    dat|=DHT11_Read_Bit();</span><br><span class="line">    &#125;						    </span><br><span class="line">    <span class="keyword">return</span> dat;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//从DHT11读取一次数据</span></span><br><span class="line"><span class="comment">//temp:温度值(范围:0~50°)</span></span><br><span class="line"><span class="comment">//humi:湿度值(范围:20%~90%)</span></span><br><span class="line"><span class="comment">//返回值：0,正常;1,读取失败</span></span><br><span class="line"></span><br><span class="line"><span class="function">u8 <span class="title">DHT11_Read_Data</span><span class="params">(u8 *humiH,u8 *humiL,u8 *tempH,u8 *tempL)</span>    </span></span><br><span class="line"><span class="function"></span>&#123;        </span><br><span class="line"> 	u8 buf[<span class="number">5</span>];</span><br><span class="line">	u8 i;</span><br><span class="line">	DHT11_Rst();</span><br><span class="line">	<span class="keyword">if</span>(DHT11_Check()==<span class="number">0</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;<span class="number">5</span>;i++)<span class="comment">//读取40位数据</span></span><br><span class="line">		&#123;</span><br><span class="line">			buf[i]=DHT11_Read_Byte();</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>((buf[<span class="number">0</span>]+buf[<span class="number">1</span>]+buf[<span class="number">2</span>]+buf[<span class="number">3</span>])==buf[<span class="number">4</span>])</span><br><span class="line">		&#123;</span><br><span class="line">			*tempH=buf[<span class="number">0</span>];			</span><br><span class="line">			*tempL=buf[<span class="number">1</span>];			</span><br><span class="line">			*humiH=buf[<span class="number">2</span>];</span><br><span class="line">			*humiL=buf[<span class="number">3</span>];</span><br><span class="line">				</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;<span class="keyword">else</span> <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;	    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="dht11驱动程序宏定义文件"><a href="#dht11驱动程序宏定义文件" class="headerlink" title="dht11驱动程序宏定义文件"></a>dht11驱动程序宏定义文件</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">ifndef</span> __DHT11_H</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> __DHT11_H	 </span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&quot;sys.h&quot;</span></span></span><br><span class="line"> </span><br><span class="line"><span class="comment">//IO方向设置</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> DHT11_IO_IN()  &#123;GPIOA-&gt;CRL&amp;=0XFFFFFFF0;GPIOA-&gt;CRL|=8;&#125;</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> DHT11_IO_OUT() &#123;GPIOA-&gt;CRL&amp;=0XFFFFFFF0;GPIOA-&gt;CRL|=3;&#125;</span></span><br><span class="line"><span class="comment">////IO操作函数											   </span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span>	DHT11_DQ_OUT PAout(0) <span class="comment">//数据端口	PA0出方向 </span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span>	DHT11_DQ_IN  PAin(0)  <span class="comment">//数据端口	PA0入方向</span></span></span><br><span class="line"></span><br><span class="line"><span class="function">u8 <span class="title">DHT11_Init</span><span class="params">(<span class="keyword">void</span>)</span></span>;<span class="comment">//初始化DHT11</span></span><br><span class="line"><span class="function">u8 <span class="title">DHT11_Read_Data</span><span class="params">(u8 *humiH,u8 *humiL,u8 *tempH,u8 *tempL)</span></span>;<span class="comment">//读取温湿度</span></span><br><span class="line"><span class="function">u8 <span class="title">DHT11_Read_Byte</span><span class="params">(<span class="keyword">void</span>)</span></span>;<span class="comment">//读出一个字节</span></span><br><span class="line"><span class="function">u8 <span class="title">DHT11_Read_Bit</span><span class="params">(<span class="keyword">void</span>)</span></span>;<span class="comment">//读出一个位</span></span><br><span class="line"><span class="function">u8 <span class="title">DHT11_Check</span><span class="params">(<span class="keyword">void</span>)</span></span>;<span class="comment">//检测是否存在DHT11</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">DHT11_Rst</span><span class="params">(<span class="keyword">void</span>)</span></span>;<span class="comment">//复位DHT11  </span></span><br><span class="line">		 				    </span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br></pre></td></tr></table></figure>

<h3 id="oled显示屏驱动程序"><a href="#oled显示屏驱动程序" class="headerlink" title="oled显示屏驱动程序"></a>oled显示屏驱动程序</h3><p>OLED的显存<br>存放格式如下.<br>[0]0 1 2 3 … 127<br>[1]0 1 2 3 … 127<br>[2]0 1 2 3 … 127<br>[3]0 1 2 3 … 127<br>[4]0 1 2 3 … 127<br>[5]0 1 2 3 … 127<br>[6]0 1 2 3 … 127<br>[7]0 1 2 3 … 127     </p>
<h5 id="向OLED写入一个字节"><a href="#向OLED写入一个字节" class="headerlink" title="向OLED写入一个字节"></a>向OLED写入一个字节</h5><p>dat:要写入的数据/命令<br>cmd:数据/命令标志 0,表示命令;1,表示数据;</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">OLED_WR_Byte</span><span class="params">(u8 dat,u8 cmd)</span></span></span><br><span class="line"><span class="function"></span>&#123;	</span><br><span class="line">	u8 i;			  </span><br><span class="line">	<span class="keyword">if</span>(cmd)</span><br><span class="line">	  OLED_DC_Set();</span><br><span class="line">	<span class="keyword">else</span> </span><br><span class="line">	  OLED_DC_Clr();		  </span><br><span class="line">	OLED_CS_Clr();</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;<span class="number">8</span>;i++)</span><br><span class="line">	&#123;			  </span><br><span class="line">		OLED_SCLK_Clr();</span><br><span class="line">		<span class="keyword">if</span>(dat&amp;<span class="number">0x80</span>)</span><br><span class="line">		   OLED_SDIN_Set();</span><br><span class="line">		<span class="keyword">else</span> </span><br><span class="line">		   OLED_SDIN_Clr();</span><br><span class="line">		OLED_SCLK_Set();</span><br><span class="line">		dat&lt;&lt;=<span class="number">1</span>;   </span><br><span class="line">	&#125;				 		  </span><br><span class="line">	OLED_CS_Set();</span><br><span class="line">	OLED_DC_Set();   	  </span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">OLED_Set_Pos</span><span class="params">(<span class="keyword">unsigned</span> <span class="keyword">char</span> x, <span class="keyword">unsigned</span> <span class="keyword">char</span> y)</span> </span></span><br><span class="line"><span class="function"></span>&#123; </span><br><span class="line">	OLED_WR_Byte(<span class="number">0xb0</span>+y,OLED_CMD);</span><br><span class="line">	OLED_WR_Byte(((x&amp;<span class="number">0xf0</span>)&gt;&gt;<span class="number">4</span>)|<span class="number">0x10</span>,OLED_CMD);</span><br><span class="line">	OLED_WR_Byte((x&amp;<span class="number">0x0f</span>)|<span class="number">0x01</span>,OLED_CMD); </span><br><span class="line">&#125;   </span><br></pre></td></tr></table></figure>

<h5 id="开启OLED显示"><a href="#开启OLED显示" class="headerlink" title="开启OLED显示"></a>开启OLED显示</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">OLED_Display_On</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	OLED_WR_Byte(<span class="number">0X8D</span>,OLED_CMD);  <span class="comment">//SET DCDC命令</span></span><br><span class="line">	OLED_WR_Byte(<span class="number">0X14</span>,OLED_CMD);  <span class="comment">//DCDC ON</span></span><br><span class="line">	OLED_WR_Byte(<span class="number">0XAF</span>,OLED_CMD);  <span class="comment">//DISPLAY ON</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="关闭OLED显示"><a href="#关闭OLED显示" class="headerlink" title="关闭OLED显示"></a>关闭OLED显示</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">OLED_Display_Off</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	OLED_WR_Byte(<span class="number">0X8D</span>,OLED_CMD);  <span class="comment">//SET DCDC命令</span></span><br><span class="line">	OLED_WR_Byte(<span class="number">0X10</span>,OLED_CMD);  <span class="comment">//DCDC OFF</span></span><br><span class="line">	OLED_WR_Byte(<span class="number">0XAE</span>,OLED_CMD);  <span class="comment">//DISPLAY OFF</span></span><br><span class="line">&#125;	</span><br></pre></td></tr></table></figure>

<h5 id="清屏函数-清完屏-整个屏幕是黑色的-和没点亮一样"><a href="#清屏函数-清完屏-整个屏幕是黑色的-和没点亮一样" class="headerlink" title="清屏函数,清完屏,整个屏幕是黑色的!和没点亮一样!!!"></a>清屏函数,清完屏,整个屏幕是黑色的!和没点亮一样!!!</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">OLED_Clear</span><span class="params">(<span class="keyword">void</span>)</span>  </span></span><br><span class="line"><span class="function"></span>&#123;  </span><br><span class="line">	u8 i,n;		    </span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;<span class="number">8</span>;i++)  </span><br><span class="line">	&#123;  </span><br><span class="line">		OLED_WR_Byte (<span class="number">0xb0</span>+i,OLED_CMD);    <span class="comment">//设置页地址（0~7）</span></span><br><span class="line">		OLED_WR_Byte (<span class="number">0x00</span>,OLED_CMD);      <span class="comment">//设置显示位置—列低地址</span></span><br><span class="line">		OLED_WR_Byte (<span class="number">0x10</span>,OLED_CMD);      <span class="comment">//设置显示位置—列高地址   </span></span><br><span class="line">		<span class="keyword">for</span>(n=<span class="number">0</span>;n&lt;<span class="number">128</span>;n++)OLED_WR_Byte(<span class="number">0</span>,OLED_DATA); </span><br><span class="line">	&#125; <span class="comment">//更新显示</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="屏幕显示"><a href="#屏幕显示" class="headerlink" title="屏幕显示"></a>屏幕显示</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br></pre></td><td class="code"><pre><span class="line"> <span class="comment">//在指定位置显示一个字符,包括部分字符</span></span><br><span class="line"><span class="comment">//x:0~127</span></span><br><span class="line"><span class="comment">//y:0~63</span></span><br><span class="line"><span class="comment">//mode:0,反白显示;1,正常显示				 </span></span><br><span class="line"><span class="comment">//size:选择字体 16/12 </span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">OLED_ShowChar</span><span class="params">(u8 x,u8 y,u8 chr)</span></span></span><br><span class="line"><span class="function"></span>&#123;      	</span><br><span class="line">	<span class="keyword">unsigned</span> <span class="keyword">char</span> c=<span class="number">0</span>,i=<span class="number">0</span>;	</span><br><span class="line">		c=chr-<span class="string">&#x27; &#x27;</span>;<span class="comment">//得到偏移后的值			</span></span><br><span class="line">		<span class="keyword">if</span>(x&gt;Max_Column<span class="number">-1</span>)&#123;x=<span class="number">0</span>;y=y+<span class="number">2</span>;&#125;</span><br><span class="line">		<span class="keyword">if</span>(SIZE ==<span class="number">16</span>)</span><br><span class="line">			&#123;</span><br><span class="line">			OLED_Set_Pos(x,y);	</span><br><span class="line">			<span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;<span class="number">8</span>;i++)</span><br><span class="line">			OLED_WR_Byte(F8X16[c*<span class="number">16</span>+i],OLED_DATA);</span><br><span class="line">			OLED_Set_Pos(x,y+<span class="number">1</span>);</span><br><span class="line">			<span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;<span class="number">8</span>;i++)</span><br><span class="line">			OLED_WR_Byte(F8X16[c*<span class="number">16</span>+i+<span class="number">8</span>],OLED_DATA);</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">else</span> &#123;	</span><br><span class="line">				OLED_Set_Pos(x,y+<span class="number">1</span>);</span><br><span class="line">				<span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;<span class="number">6</span>;i++)</span><br><span class="line">				OLED_WR_Byte(F6x8[c][i],OLED_DATA);</span><br><span class="line">				</span><br><span class="line">			&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//m^n函数</span></span><br><span class="line"><span class="function">u32 <span class="title">oled_pow</span><span class="params">(u8 m,u8 n)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	u32 result=<span class="number">1</span>;	 </span><br><span class="line">	<span class="keyword">while</span>(n--)result*=m;    </span><br><span class="line">	<span class="keyword">return</span> result;</span><br><span class="line">&#125;				  </span><br><span class="line"><span class="comment">//显示2个数字</span></span><br><span class="line"><span class="comment">//x,y :起点坐标	 </span></span><br><span class="line"><span class="comment">//len :数字的位数</span></span><br><span class="line"><span class="comment">//size:字体大小</span></span><br><span class="line"><span class="comment">//mode:模式	0,填充模式;1,叠加模式</span></span><br><span class="line"><span class="comment">//num:数值(0~4294967295);	 		  </span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">OLED_ShowNum</span><span class="params">(u8 x,u8 y,u32 num,u8 len,u8 size)</span></span></span><br><span class="line"><span class="function"></span>&#123;         	</span><br><span class="line">	u8 t,temp;</span><br><span class="line">	u8 enshow=<span class="number">0</span>;						   </span><br><span class="line">	<span class="keyword">for</span>(t=<span class="number">0</span>;t&lt;len;t++)</span><br><span class="line">	&#123;</span><br><span class="line">		temp=(num/oled_pow(<span class="number">10</span>,len-t<span class="number">-1</span>))%<span class="number">10</span>;</span><br><span class="line">		<span class="keyword">if</span>(enshow==<span class="number">0</span>&amp;&amp;t&lt;(len<span class="number">-1</span>))</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">if</span>(temp==<span class="number">0</span>)</span><br><span class="line">			&#123;</span><br><span class="line">				OLED_ShowChar(x+(size/<span class="number">2</span>)*t,y,<span class="string">&#x27; &#x27;</span>);</span><br><span class="line">				<span class="keyword">continue</span>;</span><br><span class="line">			&#125;<span class="keyword">else</span> enshow=<span class="number">1</span>; </span><br><span class="line">		 	 </span><br><span class="line">		&#125;</span><br><span class="line">	 	OLED_ShowChar(x+(size/<span class="number">2</span>)*t,y,temp+<span class="string">&#x27;0&#x27;</span>); </span><br><span class="line">	&#125;</span><br><span class="line">&#125; </span><br><span class="line"><span class="comment">//显示一个字符号串</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">OLED_ShowString</span><span class="params">(u8 x,u8 y,u8 *chr)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">unsigned</span> <span class="keyword">char</span> j=<span class="number">0</span>;</span><br><span class="line">	<span class="keyword">while</span> (chr[j]!=<span class="string">&#x27;\0&#x27;</span>)</span><br><span class="line">	&#123;		OLED_ShowChar(x,y,chr[j]);</span><br><span class="line">			x+=<span class="number">8</span>;</span><br><span class="line">		<span class="keyword">if</span>(x&gt;<span class="number">120</span>)&#123;x=<span class="number">0</span>;y+=<span class="number">2</span>;&#125;</span><br><span class="line">			j++;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//显示汉字</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">OLED_ShowCHinese</span><span class="params">(u8 x,u8 y,u8 no)</span></span></span><br><span class="line"><span class="function"></span>&#123;      			    </span><br><span class="line">	u8 t,adder=<span class="number">0</span>;</span><br><span class="line">	OLED_Set_Pos(x,y);	</span><br><span class="line">    <span class="keyword">for</span>(t=<span class="number">0</span>;t&lt;<span class="number">16</span>;t++)</span><br><span class="line">	&#123;</span><br><span class="line">		OLED_WR_Byte(Hzk[<span class="number">2</span>*no][t],OLED_DATA);</span><br><span class="line">		adder+=<span class="number">1</span>;</span><br><span class="line">    &#125;	</span><br><span class="line">	OLED_Set_Pos(x,y+<span class="number">1</span>);	</span><br><span class="line">    <span class="keyword">for</span>(t=<span class="number">0</span>;t&lt;<span class="number">16</span>;t++)</span><br><span class="line">	&#123;	</span><br><span class="line">		OLED_WR_Byte(Hzk[<span class="number">2</span>*no+<span class="number">1</span>][t],OLED_DATA);</span><br><span class="line">		adder+=<span class="number">1</span>;</span><br><span class="line">    &#125;					</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/***********功能描述：显示显示BMP图片128×64起始点坐标(x,y),x的范围0～127，y为页的范围0～7*****************/</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">OLED_DrawBMP</span><span class="params">(<span class="keyword">unsigned</span> <span class="keyword">char</span> x0, <span class="keyword">unsigned</span> <span class="keyword">char</span> y0,<span class="keyword">unsigned</span> <span class="keyword">char</span> x1, <span class="keyword">unsigned</span> <span class="keyword">char</span> y1,<span class="keyword">unsigned</span> <span class="keyword">char</span> BMP[])</span></span></span><br><span class="line"><span class="function"></span>&#123; 	</span><br><span class="line"> 	<span class="keyword">unsigned</span> <span class="keyword">int</span> j=<span class="number">0</span>;</span><br><span class="line"> 	<span class="keyword">unsigned</span> <span class="keyword">char</span> x,y;</span><br><span class="line">  </span><br><span class="line">  	<span class="keyword">if</span>(y1%<span class="number">8</span>==<span class="number">0</span>) y=y1/<span class="number">8</span>;      </span><br><span class="line">  	<span class="keyword">else</span> y=y1/<span class="number">8</span>+<span class="number">1</span>;</span><br><span class="line">	<span class="keyword">for</span>(y=y0;y&lt;y1;y++)</span><br><span class="line">	&#123;</span><br><span class="line">		OLED_Set_Pos(x0,y);</span><br><span class="line">    	<span class="keyword">for</span>(x=x0;x&lt;x1;x++)</span><br><span class="line">	    &#123;      </span><br><span class="line">	    	OLED_WR_Byte(BMP[j++],OLED_DATA);	    	</span><br><span class="line">	    &#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>

<h5 id="初始化显示屏"><a href="#初始化显示屏" class="headerlink" title="初始化显示屏"></a>初始化显示屏</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">OLED_Init</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123; 	</span><br><span class="line"> 	GPIO_InitTypeDef  GPIO_InitStructure;</span><br><span class="line">	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);	 	<span class="comment">//使能B端口时钟</span></span><br><span class="line">	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_0;	 </span><br><span class="line"> 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 	<span class="comment">//推挽输出</span></span><br><span class="line">	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;			<span class="comment">//速度50MHz</span></span><br><span class="line"> 	GPIO_Init(GPIOB, &amp;GPIO_InitStructure);	  					<span class="comment">//初始化PB0 PB1</span></span><br><span class="line"> 	GPIO_SetBits(GPIOB,GPIO_Pin_1|GPIO_Pin_0);	</span><br><span class="line"></span><br><span class="line">	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);	 	<span class="comment">//使能A端口时钟</span></span><br><span class="line">	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_7;	 </span><br><span class="line"> 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 	<span class="comment">//推挽输出</span></span><br><span class="line">	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;			<span class="comment">//速度50MHz</span></span><br><span class="line"> 	GPIO_Init(GPIOA, &amp;GPIO_InitStructure);	  					<span class="comment">//初始化PA5 PA7</span></span><br><span class="line"> 	GPIO_SetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_7);	</span><br><span class="line"></span><br><span class="line">  OLED_RST_Set();</span><br><span class="line">	delay_us(<span class="number">100</span>);</span><br><span class="line">	OLED_RST_Clr();</span><br><span class="line">	delay_us(<span class="number">200</span>);</span><br><span class="line">	OLED_RST_Set(); </span><br><span class="line">					  </span><br><span class="line">	OLED_WR_Byte(<span class="number">0xAE</span>,OLED_CMD);<span class="comment">//--turn off oled panel</span></span><br><span class="line">	OLED_WR_Byte(<span class="number">0x00</span>,OLED_CMD);<span class="comment">//---set low column address</span></span><br><span class="line">	OLED_WR_Byte(<span class="number">0x10</span>,OLED_CMD);<span class="comment">//---set high column address</span></span><br><span class="line">	OLED_WR_Byte(<span class="number">0x40</span>,OLED_CMD);<span class="comment">//--set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)</span></span><br><span class="line">	OLED_WR_Byte(<span class="number">0x81</span>,OLED_CMD);<span class="comment">//--set contrast control register</span></span><br><span class="line">	OLED_WR_Byte(<span class="number">0xCF</span>,OLED_CMD); <span class="comment">// Set SEG Output Current Brightness</span></span><br><span class="line">	OLED_WR_Byte(<span class="number">0xA1</span>,OLED_CMD);<span class="comment">//--Set SEG/Column Mapping     0xa0左右反置 0xa1正常</span></span><br><span class="line">	OLED_WR_Byte(<span class="number">0xC8</span>,OLED_CMD);<span class="comment">//Set COM/Row Scan Direction   0xc0上下反置 0xc8正常</span></span><br><span class="line">	OLED_WR_Byte(<span class="number">0xA6</span>,OLED_CMD);<span class="comment">//--set normal display</span></span><br><span class="line">	OLED_WR_Byte(<span class="number">0xA8</span>,OLED_CMD);<span class="comment">//--set multiplex ratio(1 to 64)</span></span><br><span class="line">	OLED_WR_Byte(<span class="number">0x3f</span>,OLED_CMD);<span class="comment">//--1/64 duty</span></span><br><span class="line">	OLED_WR_Byte(<span class="number">0xD3</span>,OLED_CMD);<span class="comment">//-set display offset	Shift Mapping RAM Counter (0x00~0x3F)</span></span><br><span class="line">	OLED_WR_Byte(<span class="number">0x00</span>,OLED_CMD);<span class="comment">//-not offset</span></span><br><span class="line">	OLED_WR_Byte(<span class="number">0xd5</span>,OLED_CMD);<span class="comment">//--set display clock divide ratio/oscillator frequency</span></span><br><span class="line">	OLED_WR_Byte(<span class="number">0x80</span>,OLED_CMD);<span class="comment">//--set divide ratio, Set Clock as 100 Frames/Sec</span></span><br><span class="line">	OLED_WR_Byte(<span class="number">0xD9</span>,OLED_CMD);<span class="comment">//--set pre-charge period</span></span><br><span class="line">	OLED_WR_Byte(<span class="number">0xF1</span>,OLED_CMD);<span class="comment">//Set Pre-Charge as 15 Clocks &amp; Discharge as 1 Clock</span></span><br><span class="line">	OLED_WR_Byte(<span class="number">0xDA</span>,OLED_CMD);<span class="comment">//--set com pins hardware configuration</span></span><br><span class="line">	OLED_WR_Byte(<span class="number">0x12</span>,OLED_CMD);</span><br><span class="line">	OLED_WR_Byte(<span class="number">0xDB</span>,OLED_CMD);<span class="comment">//--set vcomh</span></span><br><span class="line">	OLED_WR_Byte(<span class="number">0x40</span>,OLED_CMD);<span class="comment">//Set VCOM Deselect Level</span></span><br><span class="line">	OLED_WR_Byte(<span class="number">0x20</span>,OLED_CMD);<span class="comment">//-Set Page Addressing Mode (0x00/0x01/0x02)</span></span><br><span class="line">	OLED_WR_Byte(<span class="number">0x02</span>,OLED_CMD);<span class="comment">//</span></span><br><span class="line">	OLED_WR_Byte(<span class="number">0x8D</span>,OLED_CMD);<span class="comment">//--set Charge Pump enable/disable</span></span><br><span class="line">	OLED_WR_Byte(<span class="number">0x14</span>,OLED_CMD);<span class="comment">//--set(0x10) disable</span></span><br><span class="line">	OLED_WR_Byte(<span class="number">0xA4</span>,OLED_CMD);<span class="comment">// Disable Entire Display On (0xa4/0xa5)</span></span><br><span class="line">	OLED_WR_Byte(<span class="number">0xA6</span>,OLED_CMD);<span class="comment">// Disable Inverse Display On (0xa6/a7) </span></span><br><span class="line">	OLED_WR_Byte(<span class="number">0xAF</span>,OLED_CMD);<span class="comment">//--turn on oled panel</span></span><br><span class="line">	</span><br><span class="line">	OLED_WR_Byte(<span class="number">0xAF</span>,OLED_CMD); <span class="comment">/*display ON*/</span> </span><br><span class="line">	OLED_Clear();</span><br><span class="line">	OLED_Set_Pos(<span class="number">0</span>,<span class="number">0</span>); 	</span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>

<h3 id="oled显示宏定义文件"><a href="#oled显示宏定义文件" class="headerlink" title="oled显示宏定义文件"></a>oled显示宏定义文件</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">ifndef</span> __OLED_H</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> __OLED_H			  	 </span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&quot;stdlib.h&quot;</span>	    	</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&quot;stm32f10x.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">//OLED模式设置</span></span><br><span class="line"><span class="comment">//0:4线串行模式</span></span><br><span class="line"><span class="comment">//1:并行8080模式</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SIZE 16</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> XLevelL		0x00</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> XLevelH		0x10</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Max_Column	128</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Max_Row		64</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span>	Brightness	0xFF </span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> X_WIDTH 	128</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Y_WIDTH 	64	    </span></span><br><span class="line"></span><br><span class="line"><span class="comment">//-----------------OLED端口定义----------------  					   </span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> OLED_SCLK_Clr() GPIO_ResetBits(GPIOA,GPIO_Pin_5)<span class="comment">//CLK</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> OLED_SCLK_Set() GPIO_SetBits(GPIOA,GPIO_Pin_5)</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> OLED_SDIN_Clr() GPIO_ResetBits(GPIOA,GPIO_Pin_7)<span class="comment">//DIN</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> OLED_SDIN_Set() GPIO_SetBits(GPIOA,GPIO_Pin_7)</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> OLED_RST_Clr() GPIO_ResetBits(GPIOB,GPIO_Pin_0)<span class="comment">//RES</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> OLED_RST_Set() GPIO_SetBits(GPIOB,GPIO_Pin_0)</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> OLED_DC_Clr() GPIO_ResetBits(GPIOB,GPIO_Pin_1)<span class="comment">//DC</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> OLED_DC_Set() GPIO_SetBits(GPIOB,GPIO_Pin_1)</span></span><br><span class="line"> 		     </span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> OLED_CS_Clr()  GPIO_ResetBits(GPIOB,GPIO_Pin_12)<span class="comment">//CS</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> OLED_CS_Set()  GPIO_SetBits(GPIOB,GPIO_Pin_12)</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> OLED_CMD  0	<span class="comment">//写命令</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> OLED_DATA 1	<span class="comment">//写数据</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">//OLED控制用函数</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">OLED_WR_Byte</span><span class="params">(u8 dat,u8 cmd)</span></span>;	    </span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">OLED_Display_On</span><span class="params">(<span class="keyword">void</span>)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">OLED_Display_Off</span><span class="params">(<span class="keyword">void</span>)</span></span>;	   							   		    </span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">OLED_Init</span><span class="params">(<span class="keyword">void</span>)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">OLED_Clear</span><span class="params">(<span class="keyword">void</span>)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">OLED_DrawPoint</span><span class="params">(u8 x,u8 y,u8 t)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">OLED_Fill</span><span class="params">(u8 x1,u8 y1,u8 x2,u8 y2,u8 dot)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">OLED_ShowChar</span><span class="params">(u8 x,u8 y,u8 chr)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">OLED_ShowNum</span><span class="params">(u8 x,u8 y,u32 num,u8 len,u8 size)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">OLED_ShowString</span><span class="params">(u8 x,u8 y, u8 *p)</span></span>;	 </span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">OLED_Set_Pos</span><span class="params">(<span class="keyword">unsigned</span> <span class="keyword">char</span> x, <span class="keyword">unsigned</span> <span class="keyword">char</span> y)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">OLED_ShowCHinese</span><span class="params">(u8 x,u8 y,u8 no)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">OLED_DrawBMP</span><span class="params">(<span class="keyword">unsigned</span> <span class="keyword">char</span> x0, <span class="keyword">unsigned</span> <span class="keyword">char</span> y0,<span class="keyword">unsigned</span> <span class="keyword">char</span> x1, <span class="keyword">unsigned</span> <span class="keyword">char</span> y1,<span class="keyword">unsigned</span> <span class="keyword">char</span> BMP[])</span></span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span>  </span></span><br><span class="line">	 </span><br></pre></td></tr></table></figure>



<h3 id="土壤湿度传感器驱动程序"><a href="#土壤湿度传感器驱动程序" class="headerlink" title="土壤湿度传感器驱动程序"></a>土壤湿度传感器驱动程序</h3><h5 id="初始化ADC"><a href="#初始化ADC" class="headerlink" title="初始化ADC"></a>初始化ADC</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Adc_Init</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	GPIO_InitTypeDef  GPIO_InitStructure;</span><br><span class="line">	ADC_InitTypeDef		ADC_InitStruct;</span><br><span class="line">	</span><br><span class="line">	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_ADC1,ENABLE);</span><br><span class="line">	<span class="comment">/*PA_1设置为模拟输入*/</span></span><br><span class="line">	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;				 <span class="comment">// </span></span><br><span class="line">	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; 		 <span class="comment">//模拟输入</span></span><br><span class="line">	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 <span class="comment">//IO口速度为50MHz</span></span><br><span class="line">	GPIO_Init(GPIOA, &amp;GPIO_InitStructure);	</span><br><span class="line"></span><br><span class="line">	RCC_ADCCLKConfig(RCC_PCLK2_Div6);<span class="comment">//设置ADC分频因子</span></span><br><span class="line">	</span><br><span class="line">	ADC_DeInit(ADC1);<span class="comment">//复位ADC</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">//初始化ADC参数</span></span><br><span class="line">	ADC_InitStruct.ADC_ContinuousConvMode=DISABLE;</span><br><span class="line">	ADC_InitStruct.ADC_DataAlign=ADC_DataAlign_Right;</span><br><span class="line">	ADC_InitStruct.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None;</span><br><span class="line">	ADC_InitStruct.ADC_Mode=ADC_Mode_Independent;</span><br><span class="line">	ADC_InitStruct.ADC_NbrOfChannel=<span class="number">1</span>;</span><br><span class="line">	ADC_InitStruct.ADC_ScanConvMode=DISABLE;</span><br><span class="line">	</span><br><span class="line">	ADC_Init(ADC1, &amp;ADC_InitStruct);</span><br><span class="line">	</span><br><span class="line">	ADC_Cmd(ADC1,ENABLE);<span class="comment">//使能ADC1</span></span><br><span class="line">	</span><br><span class="line">	ADC_ResetCalibration(ADC1);	<span class="comment">//使能复位校准  </span></span><br><span class="line">	 </span><br><span class="line">	<span class="keyword">while</span>(ADC_GetResetCalibrationStatus(ADC1));	<span class="comment">//等待复位校准结束</span></span><br><span class="line">	</span><br><span class="line">	ADC_StartCalibration(ADC1);	 <span class="comment">//开启AD校准</span></span><br><span class="line"> </span><br><span class="line">	<span class="keyword">while</span>(ADC_GetCalibrationStatus(ADC1));	 <span class="comment">//等待校准结束</span></span><br><span class="line">		</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="获取ADC数据"><a href="#获取ADC数据" class="headerlink" title="获取ADC数据"></a>获取ADC数据</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">u16  <span class="title">Get_Adc</span><span class="params">(u8 ch)</span>  <span class="comment">//获取ADC数据</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	</span><br><span class="line">	ADC_RegularChannelConfig(ADC1,ch,<span class="number">1</span>, ADC_SampleTime_239Cycles5);</span><br><span class="line">	</span><br><span class="line">	ADC_SoftwareStartConvCmd(ADC1,ENABLE);</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">while</span>(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> ADC_GetConversionValue(ADC1);</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h5 id="获取土壤湿度数据并返回给主函数"><a href="#获取土壤湿度数据并返回给主函数" class="headerlink" title="获取土壤湿度数据并返回给主函数"></a>获取土壤湿度数据并返回给主函数</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">u16 <span class="title">Get_Adc_Average</span><span class="params">(u8 ch,u8 times)</span>  <span class="comment">//获取土壤湿度数据并返回给主函数</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	u32 temp_val=<span class="number">0</span>;</span><br><span class="line">	u8 t;</span><br><span class="line">	<span class="keyword">for</span>(t=<span class="number">0</span>;t&lt;times;t++)</span><br><span class="line">	&#123;</span><br><span class="line">		temp_val+=Get_Adc(ch);</span><br><span class="line">		delay_ms(<span class="number">1</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	temp_avrg=temp_val/times;</span><br><span class="line">	shidu1=(<span class="number">4092</span>-temp_avrg)/<span class="number">3292</span>*<span class="number">100</span>;</span><br><span class="line">	<span class="keyword">return</span> shidu1;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h3 id="土壤湿度传感器宏定义程序"><a href="#土壤湿度传感器宏定义程序" class="headerlink" title="土壤湿度传感器宏定义程序"></a>土壤湿度传感器宏定义程序</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">ifndef</span> __mk227_H</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> __mk227_H	</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&quot;sys.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Adc_Init</span><span class="params">(<span class="keyword">void</span>)</span></span>;</span><br><span class="line"><span class="function">u16  <span class="title">Get_Adc</span><span class="params">(u8 ch)</span></span>; </span><br><span class="line"><span class="function">u16 <span class="title">Get_Adc_Average</span><span class="params">(u8 ch,u8 times)</span></span>;</span><br><span class="line"><span class="keyword">extern</span> <span class="keyword">float</span> temp_avrg;</span><br><span class="line"><span class="keyword">extern</span> <span class="keyword">int</span> shidu;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span> </span></span><br></pre></td></tr></table></figure>

<h3 id="bh1750光照度传感器驱动程序"><a href="#bh1750光照度传感器驱动程序" class="headerlink" title="bh1750光照度传感器驱动程序"></a>bh1750光照度传感器驱动程序</h3><p>I2C的各种配置。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">	应用说明：</span></span><br><span class="line"><span class="comment">	在访问I2C设备前，请先调用 i2c_CheckDevice() 检测I2C设备是否正常，该函数会配置GPIO</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">I2C_BH1750_GPIOConfig</span><span class="params">(<span class="keyword">void</span>)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">*********************************************************************************************************</span></span><br><span class="line"><span class="comment">*	函 数 名: i2c_Delay</span></span><br><span class="line"><span class="comment">*	功能说明: I2C总线位延迟，最快400KHz</span></span><br><span class="line"><span class="comment">*	形    参：无</span></span><br><span class="line"><span class="comment">*	返 回 值: 无</span></span><br><span class="line"><span class="comment">*********************************************************************************************************</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">i2c_Delay</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">uint8_t</span> i;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/*　</span></span><br><span class="line"><span class="comment">	 	下面的时间是通过逻辑分析仪测试得到的。</span></span><br><span class="line"><span class="comment">    工作条件：CPU主频72MHz ，MDK编译环境，1级优化</span></span><br><span class="line"><span class="comment">  </span></span><br><span class="line"><span class="comment">		循环次数为10时，SCL频率 = 205KHz </span></span><br><span class="line"><span class="comment">		循环次数为7时，SCL频率 = 347KHz， SCL高电平时间1.5us，SCL低电平时间2.87us </span></span><br><span class="line"><span class="comment">	 	循环次数为5时，SCL频率 = 421KHz， SCL高电平时间1.25us，SCL低电平时间2.375us </span></span><br><span class="line"><span class="comment">	*/</span></span><br><span class="line">	<span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; <span class="number">10</span>; i++);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">*********************************************************************************************************</span></span><br><span class="line"><span class="comment">*	函 数 名: i2c_Start</span></span><br><span class="line"><span class="comment">*	功能说明: CPU发起I2C总线启动信号</span></span><br><span class="line"><span class="comment">*	形    参：无</span></span><br><span class="line"><span class="comment">*	返 回 值: 无</span></span><br><span class="line"><span class="comment">*********************************************************************************************************</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">i2c_Start</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="comment">/* 当SCL高电平时，SDA出现一个下跳沿表示I2C总线启动信号 */</span></span><br><span class="line">	BH1750_I2C_SDA_1();</span><br><span class="line">	BH1750_I2C_SCL_1();</span><br><span class="line">	i2c_Delay();</span><br><span class="line">	BH1750_I2C_SDA_0();</span><br><span class="line">	i2c_Delay();</span><br><span class="line">	BH1750_I2C_SCL_0();</span><br><span class="line">	i2c_Delay();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">*********************************************************************************************************</span></span><br><span class="line"><span class="comment">*	函 数 名: i2c_Start</span></span><br><span class="line"><span class="comment">*	功能说明: CPU发起I2C总线停止信号</span></span><br><span class="line"><span class="comment">*	形    参：无</span></span><br><span class="line"><span class="comment">*	返 回 值: 无</span></span><br><span class="line"><span class="comment">*********************************************************************************************************</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">i2c_Stop</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="comment">/* 当SCL高电平时，SDA出现一个上跳沿表示I2C总线停止信号 */</span></span><br><span class="line">	BH1750_I2C_SDA_0();</span><br><span class="line">	BH1750_I2C_SCL_1();</span><br><span class="line">	i2c_Delay();</span><br><span class="line">	BH1750_I2C_SDA_1();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">*********************************************************************************************************</span></span><br><span class="line"><span class="comment">*	函 数 名: i2c_SendByte</span></span><br><span class="line"><span class="comment">*	功能说明: CPU向I2C总线设备发送8bit数据</span></span><br><span class="line"><span class="comment">*	形    参：_ucByte ： 等待发送的字节</span></span><br><span class="line"><span class="comment">*	返 回 值: 无</span></span><br><span class="line"><span class="comment">*********************************************************************************************************</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">i2c_SendByte</span><span class="params">(<span class="keyword">uint8_t</span> _ucByte)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">uint8_t</span> i;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/* 先发送字节的高位bit7 */</span></span><br><span class="line">	<span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; <span class="number">8</span>; i++)</span><br><span class="line">	&#123;		</span><br><span class="line">		<span class="keyword">if</span> (_ucByte &amp; <span class="number">0x80</span>)</span><br><span class="line">		&#123;</span><br><span class="line">			BH1750_I2C_SDA_1();</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span></span><br><span class="line">		&#123;</span><br><span class="line">			BH1750_I2C_SDA_0();</span><br><span class="line">		&#125;</span><br><span class="line">		i2c_Delay();</span><br><span class="line">		BH1750_I2C_SCL_1();</span><br><span class="line">		i2c_Delay();	</span><br><span class="line">		BH1750_I2C_SCL_0();</span><br><span class="line">		<span class="keyword">if</span> (i == <span class="number">7</span>)</span><br><span class="line">		&#123;</span><br><span class="line">			 BH1750_I2C_SDA_1(); <span class="comment">// 释放总线</span></span><br><span class="line">		&#125;</span><br><span class="line">		_ucByte &lt;&lt;= <span class="number">1</span>;	<span class="comment">/* 左移一个bit */</span></span><br><span class="line">		i2c_Delay();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">*********************************************************************************************************</span></span><br><span class="line"><span class="comment">*	函 数 名: i2c_ReadByte</span></span><br><span class="line"><span class="comment">*	功能说明: CPU从I2C总线设备读取8bit数据</span></span><br><span class="line"><span class="comment">*	形    参：无</span></span><br><span class="line"><span class="comment">*	返 回 值: 读到的数据</span></span><br><span class="line"><span class="comment">*********************************************************************************************************</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="keyword">uint8_t</span> <span class="title">i2c_ReadByte</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">uint8_t</span> i;</span><br><span class="line">	<span class="keyword">uint8_t</span> value;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/* 读到第1个bit为数据的bit7 */</span></span><br><span class="line">	value = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; <span class="number">8</span>; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		value &lt;&lt;= <span class="number">1</span>;</span><br><span class="line">		BH1750_I2C_SCL_1();</span><br><span class="line">		i2c_Delay();</span><br><span class="line">		<span class="keyword">if</span> (BH1750_I2C_SDA_READ())</span><br><span class="line">		&#123;</span><br><span class="line">			value++;</span><br><span class="line">		&#125;</span><br><span class="line">		BH1750_I2C_SCL_0();</span><br><span class="line">		i2c_Delay();</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> value;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">*********************************************************************************************************</span></span><br><span class="line"><span class="comment">*	函 数 名: i2c_WaitAck</span></span><br><span class="line"><span class="comment">*	功能说明: CPU产生一个时钟，并读取器件的ACK应答信号</span></span><br><span class="line"><span class="comment">*	形    参：无</span></span><br><span class="line"><span class="comment">*	返 回 值: 返回0表示正确应答，1表示无器件响应</span></span><br><span class="line"><span class="comment">*********************************************************************************************************</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="keyword">uint8_t</span> <span class="title">i2c_WaitAck</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">uint8_t</span> re;</span><br><span class="line"></span><br><span class="line">	BH1750_I2C_SDA_1();	<span class="comment">/* CPU释放SDA总线 */</span></span><br><span class="line">	i2c_Delay();</span><br><span class="line">	BH1750_I2C_SCL_1();	<span class="comment">/* CPU驱动SCL = 1, 此时器件会返回ACK应答 */</span></span><br><span class="line">	i2c_Delay();</span><br><span class="line">	<span class="keyword">if</span> (BH1750_I2C_SDA_READ())	<span class="comment">/* CPU读取SDA口线状态 */</span></span><br><span class="line">		re = <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		re = <span class="number">0</span>;</span><br><span class="line">	BH1750_I2C_SCL_0();</span><br><span class="line">	i2c_Delay();</span><br><span class="line">	<span class="keyword">return</span> re;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">*********************************************************************************************************</span></span><br><span class="line"><span class="comment">*	函 数 名: i2c_Ack</span></span><br><span class="line"><span class="comment">*	功能说明: CPU产生一个ACK信号</span></span><br><span class="line"><span class="comment">*	形    参：无</span></span><br><span class="line"><span class="comment">*	返 回 值: 无</span></span><br><span class="line"><span class="comment">*********************************************************************************************************</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">i2c_Ack</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	BH1750_I2C_SDA_0();	<span class="comment">/* CPU驱动SDA = 0 */</span></span><br><span class="line">	i2c_Delay();</span><br><span class="line">	BH1750_I2C_SCL_1();	<span class="comment">/* CPU产生1个时钟 */</span></span><br><span class="line">	i2c_Delay();</span><br><span class="line">	BH1750_I2C_SCL_0();</span><br><span class="line">	i2c_Delay();</span><br><span class="line">	BH1750_I2C_SDA_1();	<span class="comment">/* CPU释放SDA总线 */</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">*********************************************************************************************************</span></span><br><span class="line"><span class="comment">*	函 数 名: i2c_NAck</span></span><br><span class="line"><span class="comment">*	功能说明: CPU产生1个NACK信号</span></span><br><span class="line"><span class="comment">*	形    参：无</span></span><br><span class="line"><span class="comment">*	返 回 值: 无</span></span><br><span class="line"><span class="comment">*********************************************************************************************************</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">i2c_NAck</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	BH1750_I2C_SDA_1();	<span class="comment">/* CPU驱动SDA = 1 */</span></span><br><span class="line">	i2c_Delay();</span><br><span class="line">	BH1750_I2C_SCL_1();	<span class="comment">/* CPU产生1个时钟 */</span></span><br><span class="line">	i2c_Delay();</span><br><span class="line">	BH1750_I2C_SCL_0();</span><br><span class="line">	i2c_Delay();	</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">*********************************************************************************************************</span></span><br><span class="line"><span class="comment">*	函 数 名: I2C_BH1750_GPIOConfig</span></span><br><span class="line"><span class="comment">*	功能说明: 配置I2C总线的GPIO，采用模拟IO的方式实现</span></span><br><span class="line"><span class="comment">*	形    参：无</span></span><br><span class="line"><span class="comment">*	返 回 值: 无</span></span><br><span class="line"><span class="comment">*********************************************************************************************************</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">I2C_BH1750_GPIOConfig</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	GPIO_InitTypeDef GPIO_InitStructure;</span><br><span class="line"></span><br><span class="line">	RCC_APB2PeriphClockCmd(BH1750_RCC_I2C_PORT, ENABLE);	<span class="comment">/* 打开GPIO时钟 */</span></span><br><span class="line"></span><br><span class="line">	GPIO_InitStructure.GPIO_Pin = BH1750_I2C_SCL_PIN | BH1750_I2C_SDA_PIN;</span><br><span class="line">	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;</span><br><span class="line">	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;  	<span class="comment">/* 开漏输出 */</span></span><br><span class="line">	GPIO_Init(BH1750_GPIO_PORT_I2C, &amp;GPIO_InitStructure);</span><br><span class="line"></span><br><span class="line">	<span class="comment">/* 给一个停止信号, 复位I2C总线上的所有设备到待机模式 */</span></span><br><span class="line">	i2c_Stop();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">*********************************************************************************************************</span></span><br><span class="line"><span class="comment">*	函 数 名: i2c_CheckDevice</span></span><br><span class="line"><span class="comment">*	功能说明: 检测I2C总线设备，CPU向发送设备地址，然后读取设备应答来判断该设备是否存在</span></span><br><span class="line"><span class="comment">*	形    参：_Address：设备的I2C总线地址</span></span><br><span class="line"><span class="comment">*	返 回 值: 返回值 0 表示正确， 返回1表示未探测到</span></span><br><span class="line"><span class="comment">*********************************************************************************************************</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="keyword">uint8_t</span> <span class="title">i2c_CheckDevice</span><span class="params">(<span class="keyword">uint8_t</span> _Address)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">uint8_t</span> ucAck;</span><br><span class="line">	i2c_Start();		<span class="comment">/* 发送启动信号 */</span></span><br><span class="line">	<span class="comment">/* 发送设备地址+读写控制bit（0 = w， 1 = r) bit7 先传 */</span></span><br><span class="line">	i2c_SendByte(_Address | BH1750_I2C_WR);</span><br><span class="line">	ucAck = i2c_WaitAck();	<span class="comment">/* 检测设备的ACK应答 */</span></span><br><span class="line"></span><br><span class="line">	i2c_Stop();			<span class="comment">/* 发送停止信号 */</span></span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> ucAck;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="BH1750读取数据"><a href="#BH1750读取数据" class="headerlink" title="BH1750读取数据"></a>BH1750读取数据</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//BH1750写一个字节</span></span><br><span class="line"><span class="comment">//返回值	成功：0		失败：非0 </span></span><br><span class="line"><span class="function"><span class="keyword">uint8_t</span> <span class="title">BH1750_Byte_Write</span><span class="params">(<span class="keyword">uint8_t</span> data)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	i2c_Start();</span><br><span class="line">	<span class="comment">//发送写地址</span></span><br><span class="line">	i2c_SendByte(BH1750_Addr|<span class="number">0</span>);</span><br><span class="line">	<span class="keyword">if</span>(i2c_WaitAck()==<span class="number">1</span>)</span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	<span class="comment">//发送控制命令</span></span><br><span class="line">	i2c_SendByte(data);</span><br><span class="line">	<span class="keyword">if</span>(i2c_WaitAck()==<span class="number">1</span>)</span><br><span class="line">		<span class="keyword">return</span> <span class="number">2</span>;</span><br><span class="line">	i2c_Stop();</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//BH1750读取测量数据</span></span><br><span class="line"><span class="comment">//返回值 成功：返回光照强度 	失败：返回0</span></span><br><span class="line"><span class="function"><span class="keyword">uint16_t</span> <span class="title">BH1750_Read_Measure</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">uint16_t</span> receive_data=<span class="number">0</span>; </span><br><span class="line">	i2c_Start();</span><br><span class="line">	<span class="comment">//发送读地址</span></span><br><span class="line">	i2c_SendByte(BH1750_Addr|<span class="number">1</span>);</span><br><span class="line">	<span class="keyword">if</span>(i2c_WaitAck()==<span class="number">1</span>)</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="comment">//读取高八位</span></span><br><span class="line">	receive_data=i2c_ReadByte();</span><br><span class="line">	i2c_Ack();</span><br><span class="line">	<span class="comment">//读取低八位</span></span><br><span class="line">	receive_data=(receive_data&lt;&lt;<span class="number">8</span>)+i2c_ReadByte();</span><br><span class="line">	i2c_NAck();</span><br><span class="line">	i2c_Stop();</span><br><span class="line">	<span class="keyword">return</span> receive_data;	<span class="comment">//返回读取到的数据</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="BH1750上电操作"><a href="#BH1750上电操作" class="headerlink" title="BH1750上电操作"></a>BH1750上电操作</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//BH1750s上电</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">BH1750_Power_ON</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	BH1750_Byte_Write(POWER_ON);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//BH1750s断电</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">BH1750_Power_OFF</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	BH1750_Byte_Write(POWER_OFF);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//BH1750复位	仅在上电时有效</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">BH1750_RESET</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	BH1750_Byte_Write(MODULE_RESET);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="BH1750初始化"><a href="#BH1750初始化" class="headerlink" title="BH1750初始化"></a>BH1750初始化</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">BH1750_Init</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	I2C_BH1750_GPIOConfig();		<span class="comment">/* 配置GPIO */</span></span><br><span class="line">	</span><br><span class="line">	BH1750_Power_ON();	<span class="comment">//BH1750s上电</span></span><br><span class="line">	<span class="comment">//BH1750_RESET();			//BH1750复位</span></span><br><span class="line">	BH1750_Byte_Write(Measure_Mode);</span><br><span class="line">	<span class="comment">//SysTick_Delay_ms(120);</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="获取光照强度"><a href="#获取光照强度" class="headerlink" title="获取光照强度"></a>获取光照强度</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">float</span> <span class="title">LIght_Intensity</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">return</span> (<span class="keyword">float</span>)(BH1750_Read_Measure()/<span class="number">1.1f</span>*Resolurtion);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="BH1750光照度传感器宏定义文件"><a href="#BH1750光照度传感器宏定义文件" class="headerlink" title="BH1750光照度传感器宏定义文件"></a>BH1750光照度传感器宏定义文件</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">ifndef</span> __BH1750_H</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> __BH1750_H	 </span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&quot;sys.h&quot;</span></span></span><br><span class="line"> </span><br><span class="line"><span class="comment">//BH1750的地址</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> BH1750_Addr				0x46</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//BH1750指令码</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> POWER_OFF					0x00</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> POWER_ON					0x01</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MODULE_RESET			0x07</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span>	CONTINUE_H_MODE		0x10</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> CONTINUE_H_MODE2	0x11</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> CONTINUE_L_MODE		0x13</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ONE_TIME_H_MODE		0x20</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ONE_TIME_H_MODE2	0x21</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ONE_TIME_L_MODE		0x23</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//测量模式</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Measure_Mode			CONTINUE_H_MODE</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//分辨率	光照强度（单位lx）=（High Byte  + Low Byte）/ 1.2 * 测量精度</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">if</span> ((Measure_Mode==CONTINUE_H_MODE2)|(Measure_Mode==ONE_TIME_H_MODE2))</span></span><br><span class="line">	<span class="meta">#<span class="meta-keyword">define</span> Resolurtion		0.5</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">elif</span> ((Measure_Mode==CONTINUE_H_MODE)|(Measure_Mode==ONE_TIME_H_MODE))</span></span><br><span class="line">	<span class="meta">#<span class="meta-keyword">define</span> Resolurtion		1</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">elif</span> ((Measure_Mode==CONTINUE_L_MODE)|(Measure_Mode==ONE_TIME_L_MODE))</span></span><br><span class="line">	<span class="meta">#<span class="meta-keyword">define</span> Resolurtion		4</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> BH1750_I2C_WR	0		<span class="comment">/* 写控制bit */</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> BH1750_I2C_RD	1		<span class="comment">/* 读控制bit */</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 定义I2C总线连接的GPIO端口, 只需要修改下面4行代码即可任意改变SCL和SDA的引脚 */</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> BH1750_GPIO_PORT_I2C	GPIOB			<span class="comment">/* GPIO端口 */</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> BH1750_RCC_I2C_PORT 	RCC_APB2Periph_GPIOB		<span class="comment">/* GPIO端口时钟 */</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> BH1750_I2C_SCL_PIN		GPIO_Pin_6			<span class="comment">/* 连接到SCL时钟线的GPIO */</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> BH1750_I2C_SDA_PIN		GPIO_Pin_7			<span class="comment">/* 连接到SDA数据线的GPIO */</span></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 定义读写SCL和SDA的宏，已增加代码的可移植性和可阅读性 */</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">if</span> 0	<span class="comment">/* 条件编译： 1 选择GPIO的库函数实现IO读写 */</span></span></span><br><span class="line">	<span class="meta">#<span class="meta-keyword">define</span> BH1750_I2C_SCL_1()  GPIO_SetBits(BH1750_GPIO_PORT_I2C, BH1750_I2C_SCL_PIN)		<span class="comment">/* SCL = 1 */</span></span></span><br><span class="line">	<span class="meta">#<span class="meta-keyword">define</span> BH1750_I2C_SCL_0()  GPIO_ResetBits(BH1750_GPIO_PORT_I2C, BH1750_I2C_SCL_PIN)		<span class="comment">/* SCL = 0 */</span></span></span><br><span class="line">	</span><br><span class="line">	<span class="meta">#<span class="meta-keyword">define</span> BH1750_I2C_SDA_1()  GPIO_SetBits(BH1750_GPIO_PORT_I2C, BH1750_I2C_SDA_PIN)		<span class="comment">/* SDA = 1 */</span></span></span><br><span class="line">	<span class="meta">#<span class="meta-keyword">define</span> BH1750_I2C_SDA_0()  GPIO_ResetBits(BH1750_GPIO_PORT_I2C, BH1750_I2C_SDA_PIN)		<span class="comment">/* SDA = 0 */</span></span></span><br><span class="line">	</span><br><span class="line">	<span class="meta">#<span class="meta-keyword">define</span> BH1750_I2C_SDA_READ()  GPIO_ReadInputDataBit(BH1750_GPIO_PORT_I2C, BH1750_I2C_SDA_PIN)	<span class="comment">/* 读SDA口线状态 */</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">else</span>	<span class="comment">/* 这个分支选择直接寄存器操作实现IO读写 */</span></span></span><br><span class="line">    <span class="comment">/*　注意：如下写法，在IAR最高级别优化时，会被编译器错误优化 */</span></span><br><span class="line">	<span class="meta">#<span class="meta-keyword">define</span> BH1750_I2C_SCL_1()  BH1750_GPIO_PORT_I2C-&gt;BSRR = BH1750_I2C_SCL_PIN				<span class="comment">/* SCL = 1 */</span></span></span><br><span class="line">	<span class="meta">#<span class="meta-keyword">define</span> BH1750_I2C_SCL_0()  BH1750_GPIO_PORT_I2C-&gt;BRR = BH1750_I2C_SCL_PIN				<span class="comment">/* SCL = 0 */</span></span></span><br><span class="line">	</span><br><span class="line">	<span class="meta">#<span class="meta-keyword">define</span> BH1750_I2C_SDA_1()  BH1750_GPIO_PORT_I2C-&gt;BSRR = BH1750_I2C_SDA_PIN				<span class="comment">/* SDA = 1 */</span></span></span><br><span class="line">	<span class="meta">#<span class="meta-keyword">define</span> BH1750_I2C_SDA_0()  BH1750_GPIO_PORT_I2C-&gt;BRR = BH1750_I2C_SDA_PIN				<span class="comment">/* SDA = 0 */</span></span></span><br><span class="line">	</span><br><span class="line">	<span class="meta">#<span class="meta-keyword">define</span> BH1750_I2C_SDA_READ()  ((BH1750_GPIO_PORT_I2C-&gt;IDR &amp; BH1750_I2C_SDA_PIN) != 0)	<span class="comment">/* 读SDA口线状态 */</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">i2c_Start</span><span class="params">(<span class="keyword">void</span>)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">i2c_Stop</span><span class="params">(<span class="keyword">void</span>)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">i2c_SendByte</span><span class="params">(<span class="keyword">uint8_t</span> _ucByte)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">uint8_t</span> <span class="title">i2c_ReadByte</span><span class="params">(<span class="keyword">void</span>)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">uint8_t</span> <span class="title">i2c_WaitAck</span><span class="params">(<span class="keyword">void</span>)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">i2c_Ack</span><span class="params">(<span class="keyword">void</span>)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">i2c_NAck</span><span class="params">(<span class="keyword">void</span>)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">uint8_t</span> <span class="title">i2c_CheckDevice</span><span class="params">(<span class="keyword">uint8_t</span> _Address)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">BH1750_Init</span><span class="params">(<span class="keyword">void</span>)</span></span>;			<span class="comment">//未包含IIC初始化</span></span><br><span class="line"><span class="function"><span class="keyword">float</span> <span class="title">LIght_Intensity</span><span class="params">(<span class="keyword">void</span>)</span></span>;	<span class="comment">//读取光照强度的值</span></span><br><span class="line"><span class="function"><span class="keyword">uint8_t</span> <span class="title">BH1750_Byte_Write</span><span class="params">(<span class="keyword">uint8_t</span> data)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">uint16_t</span> <span class="title">BH1750_Read_Measure</span><span class="params">(<span class="keyword">void</span>)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">BH1750_Power_ON</span><span class="params">(<span class="keyword">void</span>)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">BH1750_Power_OFF</span><span class="params">(<span class="keyword">void</span>)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">BH1750_RESET</span><span class="params">(<span class="keyword">void</span>)</span></span>;</span><br><span class="line">	 				    </span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="BNiot模块驱动程序"><a href="#BNiot模块驱动程序" class="headerlink" title="BNiot模块驱动程序"></a>BNiot模块驱动程序</h3><h5 id="清空缓存"><a href="#清空缓存" class="headerlink" title="清空缓存"></a>清空缓存</h5><p>//==========================================================<br>//    函数名称：    NBIOT_Clear<br>//<br>//    函数功能：    清空缓存<br>//<br>//    入口参数：    无<br>//<br>//    返回参数：    无<br>//<br>//    说明：<br>//==========================================================</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">NBIOT_Clear</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="built_in">memset</span>(nbiot_buf, <span class="number">0</span>, <span class="keyword">sizeof</span>(nbiot_buf));</span><br><span class="line">	nbiot_cnt = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="等待接收完成"><a href="#等待接收完成" class="headerlink" title="等待接收完成"></a>等待接收完成</h5><p>//==========================================================<br>//    函数名称：    NBIOT_WaitRecive<br>//<br>//    函数功能：    等待接收完成<br>//<br>//    入口参数：    无<br>//<br>//    返回参数：    REV_OK-接收完成        REV_WAIT-接收超时未完成<br>//<br>//    说明：        循环调用检测是否接收完成<br>//==========================================================</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="built_in">_Bool</span> <span class="title">NBIOT_WaitRecive</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span>(nbiot_cnt == <span class="number">0</span>) 							<span class="comment">//如果接收计数为0 则说明没有处于接收数据中，所以直接跳出，结束函数</span></span><br><span class="line">		<span class="keyword">return</span> REV_WAIT;</span><br><span class="line">		</span><br><span class="line">	<span class="keyword">if</span>(nbiot_cnt == nbiot_cntPre)				<span class="comment">//如果上一次的值和这次相同，则说明接收完毕</span></span><br><span class="line">	&#123;</span><br><span class="line">		nbiot_cnt = <span class="number">0</span>;							<span class="comment">//清0接收计数</span></span><br><span class="line">			</span><br><span class="line">		<span class="keyword">return</span> REV_OK;								<span class="comment">//返回接收完成标志</span></span><br><span class="line">	&#125;</span><br><span class="line">		</span><br><span class="line">	nbiot_cntPre = nbiot_cnt;					<span class="comment">//置为相同</span></span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> REV_WAIT;								<span class="comment">//返回接收未完成标志</span></span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="发送命令"><a href="#发送命令" class="headerlink" title="发送命令"></a>发送命令</h5><p>//==========================================================<br>//    函数名称：    NBIOT_SendCmd<br>//<br>//    函数功能：    发送命令<br>//<br>//    入口参数：    cmd：命令<br>//                res：需要检查的返回指令<br>//<br>//    返回参数：    0-成功    1-失败<br>//<br>//    说明：<br>//==========================================================</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="built_in">_Bool</span> <span class="title">NBIOT_SendCmd</span><span class="params">(<span class="keyword">char</span> *cmd, <span class="keyword">char</span> *res)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">unsigned</span> <span class="keyword">char</span> timeOut = <span class="number">200</span>;</span><br><span class="line"></span><br><span class="line">	Usart_SendString(USART2, (<span class="keyword">unsigned</span> <span class="keyword">char</span> *)cmd, <span class="built_in">strlen</span>((<span class="keyword">const</span> <span class="keyword">char</span> *)cmd));</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">while</span>(timeOut--)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span>(NBIOT_WaitRecive() == REV_OK)							<span class="comment">//如果收到数据</span></span><br><span class="line">		&#123;</span><br><span class="line">			</span><br><span class="line">			<span class="keyword">if</span>(<span class="built_in">strstr</span>((<span class="keyword">const</span> <span class="keyword">char</span> *)nbiot_buf, res) != <span class="literal">NULL</span>)		<span class="comment">//如果检索到关键词</span></span><br><span class="line">			&#123;</span><br><span class="line">				UsartPrintf(USART_DEBUG, <span class="string">&quot;%s\r\n&quot;</span>,nbiot_buf);</span><br><span class="line">				NBIOT_Clear();									<span class="comment">//清空缓存</span></span><br><span class="line">				</span><br><span class="line">				<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		delay_ms(<span class="number">10</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="发送数据"><a href="#发送数据" class="headerlink" title="发送数据"></a>发送数据</h5><p>//==========================================================<br>//    函数名称：    NBIOT_SendData<br>//<br>//    函数功能：    发送数据<br>//<br>//    入口参数：    data：数据<br>//                len：长度<br>//<br>//    返回参数：    无<br>//<br>//    说明：<br>//==========================================================</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">NBIOT_SendData</span><span class="params">(<span class="keyword">unsigned</span> <span class="keyword">char</span> *data, <span class="keyword">unsigned</span> <span class="keyword">short</span> len)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">char</span> cmdBuf[<span class="number">32</span>];</span><br><span class="line">	</span><br><span class="line">	NBIOT_Clear();								<span class="comment">//清空接收缓存</span></span><br><span class="line">	<span class="built_in">sprintf</span>(cmdBuf, <span class="string">&quot;AT+IPSEND=0,0,%s\r\n&quot;</span>, data);		<span class="comment">//发送命令</span></span><br><span class="line">	<span class="keyword">if</span>(!NBIOT_SendCmd(cmdBuf, <span class="string">&quot;&gt;&quot;</span>))				<span class="comment">//收到‘&gt;’时可以发送数据</span></span><br><span class="line">	&#123;</span><br><span class="line">		Usart_SendString(USART2,data,len);		<span class="comment">//发送设备连接请求数据</span></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h5 id="获取平台返回的数据"><a href="#获取平台返回的数据" class="headerlink" title="获取平台返回的数据"></a>获取平台返回的数据</h5><p>//==========================================================<br>//    函数名称：    NBIOT_GetIPD<br>//<br>//    函数功能：    获取平台返回的数据<br>//<br>//    入口参数：    等待的时间(乘以10ms)<br>//<br>//    返回参数：    平台返回的原始数据<br>//<br>//    说明：        不同网络设备返回的格式不同，需要去调试<br>//                如NBIOT的返回格式为    “+IPD,x:yyy”    x代表数据长度，yyy是数据内容<br>//==========================================================</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">unsigned</span> <span class="keyword">char</span> *<span class="title">NBIOT_GetIPD</span><span class="params">(<span class="keyword">unsigned</span> <span class="keyword">short</span> timeOut)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">char</span> *ptrIPD = <span class="literal">NULL</span>;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">do</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span>(NBIOT_WaitRecive() == REV_OK)								<span class="comment">//如果接收完成</span></span><br><span class="line">		&#123;</span><br><span class="line">			ptrIPD = <span class="built_in">strstr</span>((<span class="keyword">char</span> *)nbiot_buf, <span class="string">&quot;IPD,&quot;</span>);				<span class="comment">//搜索“IPD”头</span></span><br><span class="line">			<span class="keyword">if</span>(ptrIPD == <span class="literal">NULL</span>)											<span class="comment">//如果没找到，可能是IPD头的延迟，还是需要等待一会，但不会超过设定的时间</span></span><br><span class="line">			&#123;</span><br><span class="line">				<span class="comment">//UsartPrintf(USART_DEBUG, &quot;\&quot;IPD\&quot; not found\r\n&quot;);</span></span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">else</span></span><br><span class="line">			&#123;</span><br><span class="line">				ptrIPD = <span class="built_in">strchr</span>(ptrIPD, <span class="string">&#x27;:&#x27;</span>);							<span class="comment">//找到&#x27;:&#x27;</span></span><br><span class="line">				<span class="keyword">if</span>(ptrIPD != <span class="literal">NULL</span>)</span><br><span class="line">				&#123;</span><br><span class="line">					ptrIPD++;</span><br><span class="line">					<span class="keyword">return</span> (<span class="keyword">unsigned</span> <span class="keyword">char</span> *)(ptrIPD);</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="keyword">else</span></span><br><span class="line">					<span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">				</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		delay_ms(<span class="number">5</span>);</span><br><span class="line">		timeOut--;	<span class="comment">//延时等待</span></span><br><span class="line">	&#125; <span class="keyword">while</span>(timeOut &gt; <span class="number">0</span>);</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="literal">NULL</span>;														<span class="comment">//超时还未找到，返回空指针</span></span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="初始化NBIOT"><a href="#初始化NBIOT" class="headerlink" title="初始化NBIOT"></a>初始化NBIOT</h5><p>//==========================================================<br>//    函数名称：    NBIOT_Init<br>//<br>//    函数功能：    初始化NBIOT<br>//<br>//    入口参数：    无<br>//<br>//    返回参数：    无<br>//<br>//    说明：<br>//==========================================================</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">NBIOT_Init</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	</span><br><span class="line">  </span><br><span class="line">	NBIOT_Clear();</span><br><span class="line">	NBIOT_Restart();</span><br><span class="line"></span><br><span class="line">	<span class="keyword">while</span>(NBIOT_SendCmd(<span class="string">&quot;AT\r\n&quot;</span>, <span class="string">&quot;OK&quot;</span>));</span><br><span class="line">	delay_ms(<span class="number">1000</span>);	</span><br><span class="line">	NBIOT_SendCmd(<span class="string">&quot;AT+CSQ\r\n&quot;</span>, <span class="string">&quot;&quot;</span>);</span><br><span class="line">	delay_ms(<span class="number">1000</span>);</span><br><span class="line">	delay_ms(<span class="number">1000</span>);</span><br><span class="line"></span><br><span class="line">	NBIOT_SendCmd(<span class="string">&quot;AT+CSQ\r\n&quot;</span>, <span class="string">&quot;&quot;</span>);</span><br><span class="line">	delay_ms(<span class="number">1000</span>);</span><br><span class="line">	delay_ms(<span class="number">1000</span>);</span><br><span class="line">	NBIOT_SendCmd(<span class="string">&quot;AT+CGSN=1\r\n&quot;</span>,<span class="string">&quot;&quot;</span>);</span><br><span class="line">	delay_ms(<span class="number">1000</span>);</span><br><span class="line">	delay_ms(<span class="number">1000</span>);</span><br><span class="line"></span><br><span class="line">	NBIOT_SendCmd(<span class="string">&quot;AT+CIMI\r\n&quot;</span>,<span class="string">&quot;&quot;</span>);</span><br><span class="line">	delay_ms(<span class="number">1000</span>);</span><br><span class="line">	delay_ms(<span class="number">1000</span>);</span><br><span class="line"></span><br><span class="line">	NBIOT_SendCmd(<span class="string">&quot;AT+IPSTART=0,\&quot;TCP\&quot;,\&quot;39.98.198.224\&quot;,60020\r\n&quot;</span>,<span class="string">&quot;&quot;</span>);</span><br><span class="line">	delay_ms(<span class="number">1000</span>);</span><br><span class="line">	delay_ms(<span class="number">1000</span>);</span><br><span class="line"></span><br><span class="line">	NBIOT_SendCmd(<span class="string">&quot;AT+IPSTATUS=0\r\n&quot;</span>,<span class="string">&quot;&quot;</span>);</span><br><span class="line">	delay_ms(<span class="number">1000</span>);</span><br><span class="line">	delay_ms(<span class="number">1000</span>);</span><br><span class="line"></span><br><span class="line">	NBIOT_SendCmd(<span class="string">&quot;AT+IPSTATUS=0\r\n&quot;</span>,<span class="string">&quot;&quot;</span>);</span><br><span class="line">	delay_ms(<span class="number">1000</span>);</span><br><span class="line">	delay_ms(<span class="number">1000</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//	NBIOT_SendCmd(&quot;AT+IPSEND=0,0,\&quot;I am xiaochen\&quot;\r\n&quot;,&quot;&quot;);</span></span><br><span class="line"><span class="comment">//	delay_ms(1000);</span></span><br><span class="line"><span class="comment">//	delay_ms(1000);</span></span><br><span class="line">	</span><br><span class="line">	UsartPrintf(USART_DEBUG, <span class="string">&quot;NBIOT Init OK\r\n&quot;</span>);</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="开启NBIOT"><a href="#开启NBIOT" class="headerlink" title="开启NBIOT"></a>开启NBIOT</h5><p>//==========================================================<br>//    函数名称：    NBIOT_Restart<br>//<br>//    函数功能：    开启NBIOT<br>//<br>//    入口参数：    无<br>//<br>//    返回参数：    无<br>//<br>//    说明：<br>//==========================================================</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">NBIOT_Restart</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	</span><br><span class="line">	GPIO_InitTypeDef GPIO_Initure;</span><br><span class="line">	</span><br><span class="line">	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//NBIOT复位引脚</span></span><br><span class="line">	GPIO_Initure.GPIO_Mode = GPIO_Mode_Out_PP;</span><br><span class="line">	GPIO_Initure.GPIO_Pin = GPIO_Pin_14;					<span class="comment">//GPIOC14-复位</span></span><br><span class="line">	GPIO_Initure.GPIO_Speed = GPIO_Speed_50MHz;</span><br><span class="line">	GPIO_Init(GPIOC, &amp;GPIO_Initure);</span><br><span class="line">	delay_ms(<span class="number">1500</span>);</span><br><span class="line">	GPIO_WriteBit(GPIOC, GPIO_Pin_14, Bit_SET);</span><br><span class="line">	delay_ms(<span class="number">1500</span>);</span><br><span class="line">	GPIO_WriteBit(GPIOC, GPIO_Pin_14, Bit_RESET);</span><br><span class="line">	delay_ms(<span class="number">300</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="nbiot模块宏定义文件"><a href="#nbiot模块宏定义文件" class="headerlink" title="nbiot模块宏定义文件"></a>nbiot模块宏定义文件</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">ifndef</span> _NBIOT_H_</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> _NBIOT_H_</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&quot;sys.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> REV_OK		0	<span class="comment">//接收完成标志</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> REV_WAIT	1	<span class="comment">//接收未完成标志</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">NBIOT_Init</span><span class="params">(<span class="keyword">void</span>)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">NBIOT_Clear</span><span class="params">(<span class="keyword">void</span>)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">NBIOT_Restart</span><span class="params">(<span class="keyword">void</span>)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">NBIOT_SendData</span><span class="params">(<span class="keyword">unsigned</span> <span class="keyword">char</span> *data, <span class="keyword">unsigned</span> <span class="keyword">short</span> len)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="built_in">_Bool</span> <span class="title">NBIOT_SendCmd</span><span class="params">(<span class="keyword">char</span> *cmd, <span class="keyword">char</span> *res)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">unsigned</span> <span class="keyword">char</span> *<span class="title">NBIOT_GetIPD</span><span class="params">(<span class="keyword">unsigned</span> <span class="keyword">short</span> timeOut)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="程序入口函数-main-c"><a href="#程序入口函数-main-c" class="headerlink" title="程序入口函数(main.c)"></a>程序入口函数(main.c)</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&quot;sys.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&quot;delay.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&quot;usart.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&quot;led.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&quot;dht11.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&quot;timer.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&quot;oled.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&quot;string.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&quot;stdio.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&quot;mk227.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&quot;bh1750.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&quot;nbiot.h&quot;</span></span></span><br><span class="line"></span><br><span class="line">u8 humidityH;	  <span class="comment">//湿度整数部分</span></span><br><span class="line">u8 humidityL;	  <span class="comment">//湿度小数部分</span></span><br><span class="line">u8 temperatureH;   <span class="comment">//温度整数部分</span></span><br><span class="line">u8 temperatureL;   <span class="comment">//温度小数部分</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> shidu;</span><br><span class="line">u8  illumination;</span><br><span class="line"><span class="keyword">float</span> temp_avrg=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">volatile</span> u32 time; <span class="comment">//volatile  是修饰字符，不用优化</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">extern</span> <span class="keyword">void</span> <span class="title">TIM3_Int_Init</span><span class="params">(u16 arr,u16 psc)</span></span>;</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"> </span>&#123;	 </span><br><span class="line">	</span><br><span class="line">	 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);<span class="comment">//设置中断优先级分组为组2：2位抢占优先级，2位响应优先级	 </span></span><br><span class="line">	 LED_Init();		  	<span class="comment">//初始化与LED连接的硬件接口</span></span><br><span class="line">	 delay_init();	    <span class="comment">//延时函数初始化	 </span></span><br><span class="line">	 Usart1_Init(<span class="number">115200</span>);  <span class="comment">//串口1初始化</span></span><br><span class="line">	 Usart2_Init(<span class="number">115200</span>);	 <span class="comment">//串口2初始化</span></span><br><span class="line">	 DHT11_Init();      <span class="comment">//初始化DHT11</span></span><br><span class="line">	 Adc_Init();        <span class="comment">//初始化土壤湿度传感器</span></span><br><span class="line">	 delay_ms(<span class="number">1000</span>);    <span class="comment">//等待温湿度传感器度过不稳定时间</span></span><br><span class="line">	 BH1750_Init();</span><br><span class="line">	 TIM2_Int_Init(<span class="number">4999</span>,<span class="number">7199</span>);</span><br><span class="line"></span><br><span class="line">	 </span><br><span class="line">	 OLED_Init();				<span class="comment">//初始化OLED</span></span><br><span class="line"></span><br><span class="line"> </span><br><span class="line">   OLED_Clear();</span><br><span class="line"><span class="comment">//   OLED_ShowCHinese(30,0,04);//青</span></span><br><span class="line"><span class="comment">//   OLED_ShowCHinese(48,0,05);//春</span></span><br><span class="line"><span class="comment">//   OLED_ShowCHinese(66,0,06);//无</span></span><br><span class="line"><span class="comment">//   OLED_ShowCHinese(84,0,07);//限</span></span><br><span class="line"></span><br><span class="line">  	NBIOT_Init();</span><br><span class="line">		</span><br><span class="line">		TIM3_Int_Init(<span class="number">4999</span>,<span class="number">7199</span>);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">while</span>(<span class="number">1</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		 <span class="comment">//温湿度传感器相关代码</span></span><br><span class="line">		 DHT11_Read_Data(&amp;temperatureH,&amp;temperatureL,&amp;humidityH,&amp;humidityL);</span><br><span class="line">		 <span class="comment">//UsartPrintf(USART1,&quot;温度：%d.%d \t 湿度：%d.%d \t\n&quot;,temperatureH,temperatureL,humidityH,humidityL);</span></span><br><span class="line">		 delay_ms(<span class="number">1000</span>);</span><br><span class="line">		 </span><br><span class="line">		 <span class="comment">//土壤湿度相关代码	</span></span><br><span class="line">		 shidu=Get_Adc_Average(<span class="number">1</span>,<span class="number">10</span>);</span><br><span class="line">		 <span class="comment">//UsartPrintf(USART1,&quot;土壤湿度：%d %% \r\n&quot;,shidu);</span></span><br><span class="line">		 delay_ms(<span class="number">1000</span>);</span><br><span class="line">		 <span class="comment">//光照度传感器相关代码</span></span><br><span class="line">		 illumination = LIght_Intensity();</span><br><span class="line">		 <span class="comment">//UsartPrintf(USART1,&quot;光照强度：%d HX\t\n&quot;,illumination);</span></span><br><span class="line">			delay_ms(<span class="number">1000</span>);</span><br><span class="line"></span><br><span class="line">	 &#125; </span><br><span class="line">	 </span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>

<a class="btn-beautify button--animated left larger" href="https://xiaoziyanxiaoludan.gitee.io/xiaoziyan/2021/08/02/%E6%99%BA%E6%85%A7%E5%86%9C%E4%B8%9A%E7%A1%AC%E4%BB%B6%E8%AE%BE%E8%AE%A1/" 
  title="智慧农业硬件设计"><i class="far fa-hand-point-left"></i><span>智慧农业硬件设计</span></a>

<a class="btn-beautify button--animated right larger" href="https://xiaoziyanxiaoludan.gitee.io/xiaoziyan/2021/08/03/%E6%99%BA%E6%85%A7%E5%86%9C%E4%B8%9A%E9%A9%B1%E5%8A%A8%E7%A8%8B%E5%BA%8F%E5%BC%80%E5%8F%91/" 
  title="智慧农业前端设计"><i class="far fa-hand-point-right"></i><span>智慧农业前端设计</span></a>

</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">XiaoZiYan</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a target="_blank" rel="noopener" href="https://baidu.com">https://baidu.com</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://xiaoziyanxiaoludan.gitee.io/xiaoziyan" target="_blank">XiaoZiYan</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/xiaoziyan/tags/%E6%99%BA%E6%85%A7%E5%86%9C%E4%B8%9A/">智慧农业</a><a class="post-meta__tags" href="/xiaoziyan/tags/PCB/">PCB</a><a class="post-meta__tags" href="/xiaoziyan/tags/klie/">klie</a></div><div class="post_share"><div class="social-share" data-image="http://xiaoziyan.top/images/a7944f919383883f23a8b3f45788ae9c.jpeg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/xiaoziyan/2021/08/05/%E6%99%BA%E6%85%A7%E5%86%9C%E4%B8%9A%E5%89%8D%E7%AB%AF%E8%AE%BE%E8%AE%A1/"><img class="prev-cover" src="http://xiaoziyan.top/images/e610f1279851b9e6e3f4c338cfede87f.jpeg" onerror="onerror=null;src='/xiaoziyan/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">智慧农业——前端控制设计</div></div></a></div><div class="next-post pull-right"><a href="/xiaoziyan/2021/08/02/%E6%99%BA%E6%85%A7%E5%86%9C%E4%B8%9A%E7%A1%AC%E4%BB%B6%E8%AE%BE%E8%AE%A1/"><img class="next-cover" src="http://xiaoziyan.top/images/69f7d86180b45b7eabeb284a540905af.jpeg" onerror="onerror=null;src='/xiaoziyan/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">智慧农业——硬件设计</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/xiaoziyan/2021/08/05/%E6%99%BA%E6%85%A7%E5%86%9C%E4%B8%9A%E5%89%8D%E7%AB%AF%E8%AE%BE%E8%AE%A1/" title="智慧农业——前端控制设计"><img class="cover" src="http://xiaoziyan.top/images/e610f1279851b9e6e3f4c338cfede87f.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-08-05</div><div class="title">智慧农业——前端控制设计</div></div></a></div><div><a href="/xiaoziyan/2021/08/06/%E6%99%BA%E6%85%A7%E5%86%9C%E4%B8%9A%E5%A4%A7%E6%95%B0%E6%8D%AE%E8%AE%BE%E8%AE%A1/" title="智慧农业——大数据后台展示设计"><img class="cover" src="http://xiaoziyan.top/images/3205a9c3fa4e72ebd71a6101d2812929.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-08-06</div><div class="title">智慧农业——大数据后台展示设计</div></div></a></div><div><a href="/xiaoziyan/2021/08/02/%E6%99%BA%E6%85%A7%E5%86%9C%E4%B8%9A%E7%A1%AC%E4%BB%B6%E8%AE%BE%E8%AE%A1/" title="智慧农业——硬件设计"><img class="cover" src="http://xiaoziyan.top/images/69f7d86180b45b7eabeb284a540905af.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-08-02</div><div class="title">智慧农业——硬件设计</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%82%B9%E4%BA%AE%E4%B8%80%E4%B8%AALED%E7%81%AF-C"><span class="toc-number">1.</span> <span class="toc-text">点亮一个LED灯(.C)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%82%B9%E4%BA%AE%E4%B8%80%E4%B8%AALED%E7%81%AF%EF%BC%88-h%EF%BC%89"><span class="toc-number">2.</span> <span class="toc-text">点亮一个LED灯（.h）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#dht11%E6%B8%A9%E6%B9%BF%E5%BA%A6%E4%BC%A0%E6%84%9F%E5%99%A8"><span class="toc-number">3.</span> <span class="toc-text">dht11温湿度传感器</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%A4%8D%E4%BD%8DDHT11"><span class="toc-number">3.0.1.</span> <span class="toc-text">复位DHT11</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E7%AD%89%E5%BE%85DHT11%E5%9B%9E%E5%BA%94"><span class="toc-number">3.0.2.</span> <span class="toc-text">等待DHT11回应</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#dht11%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="toc-number">3.0.3.</span> <span class="toc-text">dht11初始化</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E8%AF%BB%E5%8F%96%E6%95%B0%E6%8D%AE"><span class="toc-number">3.0.4.</span> <span class="toc-text">读取数据</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#dht11%E9%A9%B1%E5%8A%A8%E7%A8%8B%E5%BA%8F%E5%AE%8F%E5%AE%9A%E4%B9%89%E6%96%87%E4%BB%B6"><span class="toc-number">4.</span> <span class="toc-text">dht11驱动程序宏定义文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#oled%E6%98%BE%E7%A4%BA%E5%B1%8F%E9%A9%B1%E5%8A%A8%E7%A8%8B%E5%BA%8F"><span class="toc-number">5.</span> <span class="toc-text">oled显示屏驱动程序</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%90%91OLED%E5%86%99%E5%85%A5%E4%B8%80%E4%B8%AA%E5%AD%97%E8%8A%82"><span class="toc-number">5.0.1.</span> <span class="toc-text">向OLED写入一个字节</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%BC%80%E5%90%AFOLED%E6%98%BE%E7%A4%BA"><span class="toc-number">5.0.2.</span> <span class="toc-text">开启OLED显示</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%85%B3%E9%97%ADOLED%E6%98%BE%E7%A4%BA"><span class="toc-number">5.0.3.</span> <span class="toc-text">关闭OLED显示</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%B8%85%E5%B1%8F%E5%87%BD%E6%95%B0-%E6%B8%85%E5%AE%8C%E5%B1%8F-%E6%95%B4%E4%B8%AA%E5%B1%8F%E5%B9%95%E6%98%AF%E9%BB%91%E8%89%B2%E7%9A%84-%E5%92%8C%E6%B2%A1%E7%82%B9%E4%BA%AE%E4%B8%80%E6%A0%B7"><span class="toc-number">5.0.4.</span> <span class="toc-text">清屏函数,清完屏,整个屏幕是黑色的!和没点亮一样!!!</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%B1%8F%E5%B9%95%E6%98%BE%E7%A4%BA"><span class="toc-number">5.0.5.</span> <span class="toc-text">屏幕显示</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%88%9D%E5%A7%8B%E5%8C%96%E6%98%BE%E7%A4%BA%E5%B1%8F"><span class="toc-number">5.0.6.</span> <span class="toc-text">初始化显示屏</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#oled%E6%98%BE%E7%A4%BA%E5%AE%8F%E5%AE%9A%E4%B9%89%E6%96%87%E4%BB%B6"><span class="toc-number">6.</span> <span class="toc-text">oled显示宏定义文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9C%9F%E5%A3%A4%E6%B9%BF%E5%BA%A6%E4%BC%A0%E6%84%9F%E5%99%A8%E9%A9%B1%E5%8A%A8%E7%A8%8B%E5%BA%8F"><span class="toc-number">7.</span> <span class="toc-text">土壤湿度传感器驱动程序</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%88%9D%E5%A7%8B%E5%8C%96ADC"><span class="toc-number">7.0.1.</span> <span class="toc-text">初始化ADC</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96ADC%E6%95%B0%E6%8D%AE"><span class="toc-number">7.0.2.</span> <span class="toc-text">获取ADC数据</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E5%9C%9F%E5%A3%A4%E6%B9%BF%E5%BA%A6%E6%95%B0%E6%8D%AE%E5%B9%B6%E8%BF%94%E5%9B%9E%E7%BB%99%E4%B8%BB%E5%87%BD%E6%95%B0"><span class="toc-number">7.0.3.</span> <span class="toc-text">获取土壤湿度数据并返回给主函数</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9C%9F%E5%A3%A4%E6%B9%BF%E5%BA%A6%E4%BC%A0%E6%84%9F%E5%99%A8%E5%AE%8F%E5%AE%9A%E4%B9%89%E7%A8%8B%E5%BA%8F"><span class="toc-number">8.</span> <span class="toc-text">土壤湿度传感器宏定义程序</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#bh1750%E5%85%89%E7%85%A7%E5%BA%A6%E4%BC%A0%E6%84%9F%E5%99%A8%E9%A9%B1%E5%8A%A8%E7%A8%8B%E5%BA%8F"><span class="toc-number">9.</span> <span class="toc-text">bh1750光照度传感器驱动程序</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#BH1750%E8%AF%BB%E5%8F%96%E6%95%B0%E6%8D%AE"><span class="toc-number">9.0.1.</span> <span class="toc-text">BH1750读取数据</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#BH1750%E4%B8%8A%E7%94%B5%E6%93%8D%E4%BD%9C"><span class="toc-number">9.0.2.</span> <span class="toc-text">BH1750上电操作</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#BH1750%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="toc-number">9.0.3.</span> <span class="toc-text">BH1750初始化</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E5%85%89%E7%85%A7%E5%BC%BA%E5%BA%A6"><span class="toc-number">9.0.4.</span> <span class="toc-text">获取光照强度</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#BH1750%E5%85%89%E7%85%A7%E5%BA%A6%E4%BC%A0%E6%84%9F%E5%99%A8%E5%AE%8F%E5%AE%9A%E4%B9%89%E6%96%87%E4%BB%B6"><span class="toc-number">10.</span> <span class="toc-text">BH1750光照度传感器宏定义文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#BNiot%E6%A8%A1%E5%9D%97%E9%A9%B1%E5%8A%A8%E7%A8%8B%E5%BA%8F"><span class="toc-number">11.</span> <span class="toc-text">BNiot模块驱动程序</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%B8%85%E7%A9%BA%E7%BC%93%E5%AD%98"><span class="toc-number">11.0.1.</span> <span class="toc-text">清空缓存</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E7%AD%89%E5%BE%85%E6%8E%A5%E6%94%B6%E5%AE%8C%E6%88%90"><span class="toc-number">11.0.2.</span> <span class="toc-text">等待接收完成</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%8F%91%E9%80%81%E5%91%BD%E4%BB%A4"><span class="toc-number">11.0.3.</span> <span class="toc-text">发送命令</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%8F%91%E9%80%81%E6%95%B0%E6%8D%AE"><span class="toc-number">11.0.4.</span> <span class="toc-text">发送数据</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E5%B9%B3%E5%8F%B0%E8%BF%94%E5%9B%9E%E7%9A%84%E6%95%B0%E6%8D%AE"><span class="toc-number">11.0.5.</span> <span class="toc-text">获取平台返回的数据</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%88%9D%E5%A7%8B%E5%8C%96NBIOT"><span class="toc-number">11.0.6.</span> <span class="toc-text">初始化NBIOT</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%BC%80%E5%90%AFNBIOT"><span class="toc-number">11.0.7.</span> <span class="toc-text">开启NBIOT</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#nbiot%E6%A8%A1%E5%9D%97%E5%AE%8F%E5%AE%9A%E4%B9%89%E6%96%87%E4%BB%B6"><span class="toc-number">12.</span> <span class="toc-text">nbiot模块宏定义文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%A8%8B%E5%BA%8F%E5%85%A5%E5%8F%A3%E5%87%BD%E6%95%B0-main-c"><span class="toc-number">13.</span> <span class="toc-text">程序入口函数(main.c)</span></a></li></ol></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By XiaoZiYan</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><p><a style="margin-inline:5px"target="_blank" href="https://hexo.io/"> <img src="https://img.shields.io/badge/Frame-Hexo-blue?style=flat&logo=hexo" title="博客框架为 Hexo" alt="HEXO"></a><a style="margin-inline:5px"target="_blank"href="https://butterfly.js.org/"> <img src="https://img.shields.io/badge/Theme-Butterfly-6513df?style=flat&logo=bitdefender" title="主题采用 Butterfly" alt="Butterfly"></a><a style="margin-inline:5px"target="_blank" href="https://www.jsdelivr.com/"> <img src="https://img.shields.io/badge/CDN-jsDelivr-orange?style=flat&logo=jsDelivr" title="本站使用 Jsdelivr 为静态资源提供CDN加速" alt="Jsdelivr"></a><a style="margin-inline:5px"target="_blank" href="https://github.com/"> <img src="https://img.shields.io/badge/Source-gitee-d021d6?style=flat&logo=Gitee" title="本站项目由 Gitee 托管" alt="Gitee"></a><a style="margin-inline:5px"target="_blank"href="http://creativecommons.org/licenses/by-nc-sa/4.0/"> <img src="https://img.shields.io/badge/Copyright-BY--NC--SA%204.0-d42328?style=flat&logo=Claris" alt="img" title="本站采用知识共享署名-非商业性使用-相同方式共享4.0国际许可协议进行许可"></a></p></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/xiaoziyan/js/utils.js"></script><script src="/xiaoziyan/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script>function panguFn () {
  if (typeof pangu === 'object') pangu.autoSpacingPage()
  else {
    getScript('https://cdn.jsdelivr.net/npm/pangu/dist/browser/pangu.min.js')
      .then(() => {
        pangu.autoSpacingPage()
      })
  }
}

function panguInit () {
  if (false){
    GLOBAL_CONFIG_SITE.isPost && panguFn()
  } else {
    panguFn()
  }
}

document.addEventListener('DOMContentLoaded', panguInit)</script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"></div><script src="./js/sakura.js"></script><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="true"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><script data-pjax>function electric_clock_injector_config(){
                var parent_div_git = document.getElementsByClassName('sticky_layout')[0];
                var item_html = '<div class="card-widget card-clock"><div class="card-glass"><div class="card-background"><div class="card-content"><div id="hexo_electric_clock"><img id="card-clock-loading" src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-clock/clock/images/weather/loading.gif" style="height: 120px; width: 100%;" data-ll-status="loading" class="entered loading"></div></div></div></div></div>';
                console.log('已挂载electric_clock')
                // parent_div_git.innerHTML=item_html+parent_div_git.innerHTML // 无报错，但不影响使用(支持pjax跳转)
                parent_div_git.insertAdjacentHTML("afterbegin",item_html) // 有报错，但不影响使用(支持pjax跳转)
            }if( document.getElementsByClassName('sticky_layout')[0] && (location.pathname ==='all'|| 'all' ==='all')){

            electric_clock_injector_config()
        } </script><script src="https://pv.sohu.com/cityjson?ie=utf-8"></script><script data-pjax  src="https://cdn.jsdelivr.net/gh/Zfour/hexo-electric-clock@1.0.6/clock.js"></script><!-- hexo injector body_end end --></body></html>